我是Powershell的新手,但我已經盡力了。 我試圖創建一個腳本來將文件複製到陣列中所有XP機器的所有用戶桌面。該腳本基本上說:「如果機器是可pingable的,則複製該文件,如果沒有,則不要。」然後我想將這些信息導出爲CSV文件以供進一步分析。Powershell從ForEach Loop導出爲CSV
我已經設置了所有,但不管我做什麼,它只會導出它運行的最後一臺PC。它似乎貫穿所有PC(經過輸出到txt文件的測試),但它不會將所有機器記錄到CSV。任何人都可以提供任何建議嗎?
$ArrComputers = 「PC1", "PC2", "PC3"
foreach ($Computer in $ArrComputers) {
$Reachable = Test-Connection -Cn $Computer -BufferSize 16 -Count 1 -ea 0 -quiet
$Output = @()
#Is the machine reachable?
if($Reachable)
{
#If Yes, copy file
Copy-Item -Path "\\servername\filelocation" -Destination "\\$Computer\c$\Documents and Settings\All Users\Desktop\filename"
$details = "Copied"
}
else
{
#If not, don't copy the file
$details = "Not Copied"
}
#Store the information from this run into the array
$Output =New-Object -TypeName PSObject -Property @{
SystemName = $Computer
Reachable = $reachable
Result = $details
} | Select-Object SystemName,Reachable,Result
}
#Output the array to the CSV File
$Output | Export-Csv C:\GPoutput.csv
Write-output "Script has finished. Please check output files."
我試過把$輸出| Export-CSV C:\ GPoutput.csv放入數組中,但它再次覆蓋自身,因爲不允許使用-append。 我也嘗試將整個foreach循環保存到一個變量中,但這帶來了錯誤:「in expression or statement」中的「意外標記」。 – Groveham
您必須使用v2。我建議然後將整個foreach循環存儲到$ Output和export中。 –