[英]Powershell Out-file not working
我有一個Powershell腳本,該腳本在一些遠程服務器上收集一些文件信息。 當寫入控制台時,我看到結果很好,但是它們似乎沒有寫入指定的輸出文件中:
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
您可以在兩個地方使用相同的變量$filepath
:作為foreach
循環的循環變量和循環外的循環變量。 循環結束后, foreach
循環不會恢復循環變量的值。 因此,在循環結束后,循環變量將具有循環的最后一個值或由break
命令中斷循環的值。 如果您不希望變量值被foreach
循環覆蓋,則應為循環變量選擇其他名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.