首页 > 解决方案 > Powershell 从多台远程计算机、所有驱动器中查找多个文件

问题描述

需要制作一个从多台计算机列表中查找文件列表的脚本。下面的脚本可以正常工作。现在需要添加一个功能,以便脚本通过每台计算机的所有驱动器,而不仅仅是 C$。问题是我不知道哪些电脑有哪些驱动器..

当前脚本:

$computers = Get-Content .\computers.txt

$filenames = Get-Content .\filenamelist.txt

foreach ($computer in $computers) {
    foreach ($filename in $filenames) {
        Get-ChildItem -Recurse -Force \\$computer\c$ -ErrorAction SilentlyContinue |
        Where-Object { ($_.PSIsContainer -eq $false) -and ( $_.Name -eq "$filename") } |
        Select-Object Name, Directory | 
        Export-Csv .\FoundFiles.csv -nti -append
    }
}

所以我应该以某种方式实现命令行:

$Drives = Get-PSDrive -PSProvider 'FileSystem'

这样它就可以在每台计算机上运行,​​并根据结果运行 Get-ChildItem 行,用于每台远程计算机上的每个现有驱动器。

请问有什么想法吗?

标签: powershellforeachget-childitem

解决方案


推荐阅读