0
我有一个powershell脚本,它收集某些远程服务器上的某些文件信息。 当写入控制台,我看到的结果不错,但他们似乎并不被获取写入到指定了文件:Powershell Out-file不起作用
out-file -filepath $filepath -inputobject $date -force -encoding ASCII -width 50
ForEach($server in $serverLists)
{
$result += $server
$result += $break
ForEach($filePath in $filePaths)
{
$result += $filePath
$result += $break
$command = "forfiles /P " + $filePath + " /S /D -1 > C:\temp\result.txt"
$cmd = "cmd /c $command"
Invoke-WmiMethod -class Win32_process -name Create -ArgumentList $cmd -ComputerName $server
sleep 2
$result += Get-Content \\$server\C$\temp\result.txt
$result += $break
$result += $break
write-host $result
#out-file -filepath $filepath -Append -inputobject $result -encoding ASCII -width 200
Remove-Item \\$server\C$\temp\result.txt
}
$result += $break
}
out-file -filepath $filepath -Append -inputobject $result -encoding ASCII -width 200
您正在使用哪个版本的PowerShell? – Leptonator
使用'$ filepath'作为内部'foreach'循环的循环变量。你也可以在foreach循环之前使用'$ filepath'变量。它应该是两个不同名称的变量吗? – PetSerAl
@PetSerAl,这是问题(我认为变量是区分大小写的) 请作出回应,以便我可以将其标记为正确的答案。 –