【发布时间】:2016-03-13 13:51:12
【问题描述】:
我是 Powershell 的新手,但我已经尽力了。 我正在尝试创建一个脚本来将文件复制到阵列中所有 XP 机器的所有用户桌面。该脚本基本上是说“如果机器可以ping通,请复制文件,如果不能,则不要。”然后我想将此信息导出到 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."
【问题讨论】:
标签: powershell csv export-csv