我有一个路径列表和一个可执行文件列表,我需要将它们连接在一起,并在每个所述路径下查找每个文件。然而,当我运行我的代码时,我看到可执行文件并没有被连接,只是作为一个新的行添加。此外,它只被添加到列表中的最后一个对象中。请参见下面的代码、输出和期望输出:
代码:
$final_paths = @();
$paths = @("C:\Program Files\Microsoft Office\Office15", "C:\Program Files\Microsoft
Office Servers\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 ($exe in $exes)
{
$final_paths += $paths"\$exe";
write-output $final_paths;
}
foreach ($found_path in $final_paths)
{
$file = Get-Item -Path $found_path -ErrorAction Ignore;
Write-Output $file
样本输出:
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
Excel.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
ExcelCnv.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
ExcelViewer.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
PowerPoint.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
PowerPointViewer.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
PowerPointCnv.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
Publisher.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
Project.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
OneNote.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
InfoPath.exe Groove.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
FrontPage.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
SharePointDesigner.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
Visio.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
VisioViewer.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
Lync.exeOutlook.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
WINPROJ
...
...
...
you get the idea
期望产出:
我只需要作为字符串返回的文件名,如下所示:
WIINPROJ
发布于 2022-01-11 12:37:42
为什么要尝试将路径连接到全名,然后尝试使用Get-Item
来确定该文件是否存在?
使用得到-儿童要容易得多,因为
Where-Object
对象,因此以后不需要检查它们是否存在。$paths = 'C:\Program Files\Microsoft Office\Office15', 'C:\Program Files\Microsoft Office Servers\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'
$files = Get-ChildItem -Path $paths -File | Where-Object { $exes -contains $_.Name }
现在决定要从$files集合中检索哪个属性。
# just the Name?
$files.Name | Select-Object -Unique # unique in case you found the same exe in both folders
# the Fullname perhaps?
$files.FullName
发布于 2022-01-10 22:58:23
您可以使用来自Path.Combine
的System.IO
方法进行如下操作:
$finalPaths = foreach($path in $paths)
{
foreach($exe in $exes)
{
Get-Item ([System.IO.Path]::Combine($path, $exe)) -EA Ignore
}
}
$finalPaths.Name # => Should be the Names of the files found
或者,正如戴利在他的评论中所建议的那样,您可以使用Join-Path
实现相同的结果:
$finalPaths = foreach($path in $paths)
{
foreach($exe in $exes)
{
Get-Item (Join-Path $path -ChildPath $exe) -EA Ignore
}
}
https://stackoverflow.com/questions/70659767
复制相似问题