使用PowerShell的ForEach循环导出到CSV

8

我对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."   
3个回答

10
这个问题是这样的:
#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

您的foreach循环的每次迭代都保存到 $Output。覆盖以前存在的内容,即上一次迭代的内容。这意味着只有最后一次迭代保存到$Output并导出。因为您正在运行PowerShell v2,我建议将整个foreach循环保存到变量中并导出。

$Output = foreach ($Computer in $ArrComputers) {
  New-Object -TypeName PSObject -Property @{
    SystemName = $Computer
    Reachable = $reachable 
    Result = $details
  } | Select-Object SystemName,Reachable,Result
}
$Output | Export-Csv C:\GPoutput.csv

我尝试将 $Output | Export-CSV C:\GPoutput.csv 移动到数组中,但由于没有 -append 允许,它在每次传递时都会覆盖自身。我还尝试将整个 foreach 循环保存到变量中,但这会带来错误:"表达式或语句中的意外标记 'in'。" - Groveham
你必须使用v2版本。我建议将整个foreach循环存储到$Output中,然后导出它。 - Benjamin Hubbard

4

您可以使用“Export-CSV”命令将数据追加到 CSV 文件中,以下是示例:

foreach ($item in $ITGlueTest.data)
{
$item.attributes | export-csv C:\organization.csv -Append
} 

1

这是代码。使用PSCustomObjectNew-Object更快地枚举数据。每次循环后将附加到.csv文件,因此不会覆盖以前的数据。

foreach ($Computer in $ArrComputers) {

$Reachable = Test-Connection -Cn $Computer -BufferSize 16 -Count 1 -ea 0 -quiet

#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  
       [PSCustomObject]@{
       SystemName = $Computer
       Reachable = $reachable 
       Result = $details
       } | Export-Csv C:\yourcsv.csv -notype -Append 
}  

这不会起作用,因为他正在使用v2。PSCustomObject和-Append是v3+的。 - Benjamin Hubbard
错过了他提到XP机器的那一部分。 - bentek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接