【发布时间】:2022-01-12 00:13:21
【问题描述】:
我有一个路径数组和一个我要匹配的 exe 数组,并获取每个路径下匹配的任何 exe 的名称。我知道事实上没有重复的条目,并且找到的 exe 仅存在于数组中的一个路径下。但是,当我执行Sort-Object -Unique 时,会出现重复项并且不会被删除。
代码:
$found_paths =@("C:\Program Files\Microsoft Office Servers\OFFICE15", "C:\Program Files\Microsoft Office\Office15");
$exes = @("MSOCF.DLL", "access.exe", "word.exe", "wordCnv.exe", "WordViewer.exe", "Excel.exe", "ExcelCnv.exe", "ExcelViewer.exe", "PowerPoint.exe",
"PowerPointViewer.exe", "PowerPointCnv.exe", "Publisher.exe", "Project.exe", "OneNote.exe", "InfoPath.exe Groove.exe", "FrontPage.exe",
"SharePointDesigner.exe", "Visio.exe", "VisioViewer.exe", "Lync.exeOutlook.exe", "WINPROJ.EXE");
foreach($path in $found_paths)
{
foreach($exe in $exes)
{
$found_files = Get-Item ([System.IO.Path]::Combine($path, $exe)) -EA Ignore;
$found_file = $found_files.Name | Sort-Object -Unique;
$found_file
}
}
输出:
MSOCF.DLL
WINPROJ.EXE
MSOCF.DLL
WINPROJ.EXE
【问题讨论】:
-
仔细检查是否没有尾随空格字符。
-
要查看发生了什么,请将
$found_files.Name更改为$found_files.FullName。
标签: windows powershell loops