我正在尝试创建一个ps1,它可以搜索多个服务器列表中的多个文件夹,但似乎不起作用。我想*文件夹有问题。对不起,我对此很陌生。
$folders = get-content "C:tempfolders.txt"
get-content c:tempservers.txt | Foreach {
Get-ChildItem -Path "c:temp" -include *folders -Recurse -ErrorAction
silentlycontinue} | export-csv c:Tempresults.csv
您正在读取一个文本文件,其中(可能(有一个要探测的服务器名称列表,但在您的代码中,除了迭代该列表之外,您什么也不做。。
尝试
$folders = Get-Content 'C:tempfolders.txt' # the list of foldernames to look for
Get-Content 'C:tempservers.txt' | ForEach-Object {
# construct a UNC path to the C:Temp folder on the remote server (\serverc$temp)
# the $_ Automatic variable contains one servername in each iteration
$remotePath = "\$_C$temp"
Get-ChildItem -Path $remotePath -Include $folders -Directory -Recurse -ErrorAction SilentlyContinue |
# select properties you need
Select-Object @{Name = 'ComputerName'; Expression = {$_}}, Name, FullName, CreationTime, LastAccessTime, LastWriteTime
} | Export-Csv 'C:tempresults.csv' -NoTypeInformation
或
让远程服务器完成工作并将结果返回给您。您可能需要在调用命令上添加-Credentials:
$folders = Get-Content 'C:tempfolders.txt' # the list of foldernames to look for
Get-Content 'C:tempservers.txt' | ForEach-Object {
Invoke-Command -ComputerName $_ -ScriptBlock {
# this is running on the remote computer, so it uses it's own LOCAL path
# the $folders variable needs to be scoped '$using:folders', otherwise it is unknown in the scriptblock
Get-ChildItem -Path 'C:temp' -Include $using:folders -Directory -Recurse -ErrorAction SilentlyContinue |
# select and output the properties you need
Select-Object @{Name = 'ComputerName'; Expression = {$env:COMPUTERNAME}}, Name, FullName, CreationTime, LastAccessTime, LastWriteTime
}
} | Export-Csv 'C:tempresults.csv' -NoTypeInformation