我正在尝试在远程服务器列表中查找文件目录。我想要:
- 使用 Powershell;从逗号分隔的文本文件中获取目标,即 server1,file1 注意:每个服务器的文件都不同。
- 在 C: 和 E: 分区中搜索某个文件,即使该文件有多个实例
- 获取文件目录
- 将结果写入 .csv
- 在 Powershell 主窗口上查看输出
这是我一直在使用的脚本。我无法让它正常工作。我需要一些帮助:
$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath
$FileName = "$dir\Servers.txt"
$Data = gc $FileName
foreach ($Row in $Data)
{
$Position = $Row.IndexOf(",")
$Server = $Row.Substring(0, $Position)
$File = $Row.Substring($Position + 1)
$CPart = @{
Name = "CPart"
Path = "\\$Server\C$"
#Path = "FileSystem::\\$Server\C$"
}
$EPart = @{
Name = "EPart"
Path = "\\$Server\E$"
#Path = "FileSystem::\\$Server\E$"
}
@($CPart, $EPart) | ForEach-Object {
if ($Server -like "*.mydomain.com") {$session = New-PSSession -Computer $Server -Credential MYDOMAIN\userid -ErrorAction Stop}
else {$Session = New-PSSession -Computer $Server -ErrorAction Stop}
Invoke-Command -Session $Session -ScriptBlock {$Result = Get-ChildItem -Recurse -Path $_.Path | Where-Object {$_.Name -match '$File'};return $Result}
$Result
#$item = Get-ChildItem -Recurse -Path $_.Path | Where-Object {$_.Name -match '$File' -and }
#Write-Host $item
}
}
<#foreach ($Server in $Servers)
{
"\\$Server\C$" | Get-ChildItem -recurse -filter $FileList | Select Directory, Name | Write-Host $_.Directory + "\" + $_.Name
"\\$Server\E$" | Get-ChildItem -recurse -filter $FileList | Select Directory, Name | Write-Host $_.Directory + "\" + $_.Name
}
$columnToGet = 0
$columns = gc $fileName |
%{ $_.Split(",")[$columnToGet] }
$columns
#>