【问题标题】:Powershell Export to CSV from ForEach LoopPowershell 从 ForEach 循环导出到 CSV
【发布时间】: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


    【解决方案1】:

    问题是这样的:

    #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 循环保存到一个变量中,但这会带来错误:“表达式或语句中出现意外的标记。”
    • 您必须使用 v2。我建议然后将整个 foreach 循环存储到 $Output 并导出它。
    【解决方案2】:

    您可能希望附加 export-csv 以将项目添加到 csv 文件 这是一个例子

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

    【讨论】:

      【解决方案3】:

      给你。这使用了PSCustomObject,它比New-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+。
      • 错过了他提到 XP 机器的部分。
      猜你喜欢
      • 2016-07-11
      • 1970-01-01
      • 2014-12-05
      • 2018-03-13
      • 1970-01-01
      • 1970-01-01
      • 2015-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多