我正在尝试登录多个服务器,然后从这些服务器获取文件列表。
以下是我的剧本:
$ServerName=Get-content "D:\HOMEWARE\BLRMorningCheck\Jerry\servername.txt"
foreach ($server in $ServerName)
{
$server_host=echo $server | %{$data = $_.split(";"); Write-Output "$($data[0])"}
$Targetfolder=echo $server | %{$data = $_.split(";"); Write-Output "$($data[1])"}
$Extension =@("*.log","*.txt*")
$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse
echo $Files
}
当我运行调试模式时,我发现它确实没有选择文件。
错误:
Get-ChildItem : Illegal characters in path.
At D:\HOMEWARE\BLRMorningCheck\Jerry\test.ps1:14 char:23
+ $Files = Get-Childitem <<<< $TargetFolder -Include $Extension -Recurse
+ CategoryInfo : InvalidArgument: (D:\HOMEWARE\BLR...ck\Jerry\Check":String) [Get-ChildItem], ArgumentException
+ FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.GetChildItemCommand
Get-ChildItem : Cannot find path 'D:\HOMEWARE\BLRMorningCheck\Jerry\"\srvparrtfh01\d$\HOMEWARE\BLRMorningCheck\Jerry\Check"' because it does not exist.
At D:\HOMEWARE\BLRMorningCheck\Jerry\test.ps1:14 char:23
+ $Files = Get-Childitem <<<< $TargetFolder -Include $Extension -Recurse
+ CategoryInfo : ObjectNotFound: (D:\HOMEWARE\BLR...ck\Jerry\Check":String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
注意:如果我手动运行,例如
$Files = Get-Childitem \\servername\d$\HOMEWARE\BLRMorningCheck\Jerry\Check -Include "*.log","*.txt*" -Recurse
我得到了输出。
答案 0 :(得分:3)
在评论中提及@ arco444和@PetSerAl:您的路径字符串具有前导/尾随双引号,它们是路径中的无效字符。您需要删除它们以使路径按预期工作:
$Files = Get-Childitem ($TargetFolder -replace '^"|"$') -Include $Extension -Recurse
当然,由于您的输入文件似乎是某种CSV,因此您可以使用Import-Csv
而不是使用Get-Content
读取文件并手动拆分字段。那已经处理了文件导入的双引号。
$filename = 'D:\HOMEWARE\BLRMorningCheck\Jerry\servername.txt'
$extension = '*.log', '*.txt*'
Import-Csv $filename -Delimiter ';' -Header 'ComputerName', 'TargetFolder' | % {
Get-Childitem $_.TargetFolder -Include $extension -Recurse
}