我对PowerShell不太了解,但我已经尽力了。我正在尝试创建一个脚本,将文件复制到数组中所有XP计算机的所有用户桌面上。该脚本基本上是这样说的:“如果可以ping通该计算机,则复制文件;否则,不进行操作。”然后,我想将此信息导出到CSV文件以供进一步分析。
我已经设置好了所有内容,但无论我做什么,它都只会导出最后一台计算机。它似乎通过所有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."