如何为每个对象循环导出变量的 csv

How to export csv of variables for each object loop

一个月来,我一直坚持尝试以该脚本所需的格式导出。我不知道如何让它在两个单独的循环中将这些变量导出到一个 .csv 文件中。我们得到一个包含 staging_inputstaging_location 字段的 .csv,这两个字段都包含网络上的文件位置。我们需要比较这两者以确保它们的文件数量和大小相同。我为这些字段中的每一个创建了一个 ForEach-Object 循环,这给了我想要的输出,但我无法在每个循环结束时 export-csv 到一个 .csv.

Desired final output

Current output

#User input CSV File and Workorder
$workorder =  Read-Host -Prompt 'Please enter the Fq job code'
   $pos = $workorder.IndexOf("_")
   $Client = $workorder.Substring(0, $pos)
   $Matter = $workorder.Substring(6, $pos)
   $job = $workorder.Substring($pos+7)

$csvoutputpath = "\ldthost.pvt\client\" + $Client + "\" + $Matter + "\Proc\WKP\" + $job
$outputfilename = $workorder + ".csv"
$csvinputpath = Read-Host -Prompt 'Please Input the directory of the CSV file containing Staging input and Staging location'

$staginginput = Import-Csv $csvinputpath | select -ExpandProperty staging_input 
$staginginputpath = $staginginput.TrimStart("\")
    #Get Child Item for each line
    $staginginputpath | ForEach-Object{
       # In here, we do whatever want to the 'cell' that's currently in the pipeline
       # Get File Counts
       $staginginputcount = (Get-ChildItem -Recurse -File -Force -LiteralPath \?\UNC$_ | Measure-Object).Count

       #Get size
       $staginginputsize = Get-ChildItem -Recurse -File -Force -LiteralPath \?\UNC$_ | Measure-Object -property length -sum
       $staginginputsize = $staginginputsize.sum / 1KB
   
   }

$staginglocation = Import-Csv $csvinputpath | select -ExpandProperty staging_location
$staginglocationpath = $staginglocation.TrimStart("\")
   #Get Child Item for each line
   $staginglocationpath | ForEach-Object{
      # In here, we do whatever want to the 'cell' that's currently in the pipeline
      # Get File Counts
      $staginglocationcount = (Get-ChildItem -Recurse -File -Force -LiteralPath \?\UNC$_ | Measure-Object).Count
      
      #Get size
      $staginglocationsize = Get-ChildItem -Recurse -File -Force -LiteralPath \?\UNC$_ | Measure-Object -property length -sum
      $staginglocationsize = $staginglocationsize.sum / 1KB
  }

  ##Export Final Output
  $data = @()
  $row = New-Object PSObject
  $row | Add-Member -MemberType NoteProperty -Name "staging_input" -Value $staginginput
  $row | Add-Member -MemberType NoteProperty -Name "staging_location" -Value $staginglocation
  $row | Add-Member -MemberType NoteProperty -Name "staging_input_File_Count" -Value $staginginputcount
  $row | Add-Member -MemberType NoteProperty -Name "staging_location_File_Count" -Value $staginglocationcount
  $row | Add-Member -MemberType NoteProperty -Name "staging_input_File_Size" -Value $staginginputsize
  $row | Add-Member -MemberType NoteProperty -Name "staging_location_File_Size" -Value $staginglocationsize
  $data += $row
  $Finaloutput = $csvoutputpath + "\" + $outputfilename
  $data | Export-Csv $Finaloutput -NoTypeInformation -Append

使用一个循环而不是两个循环:

#User input CSV File and Workorder
$workorder = Read-Host -Prompt 'Please enter the Fq job code'
$pos = $workorder.IndexOf("_")
$Client = $workorder.Substring(0, $pos)
$Matter = $workorder.Substring(6, $pos)
$job = $workorder.Substring($pos + 7)

$csvoutputpath = "\ldthost.pvt\client\" + $Client + "\" + $Matter + "\Proc\WKP\" + $job
$outputfilename = $workorder + ".csv"
$csvinputpath = Read-Host -Prompt 'Please Input the directory of the CSV file containing Staging input and Staging location'

$Finaloutput = $csvoutputpath + "\" + $outputfilename

Import-Csv $csvinputpath |ForEach-Object {
    # Calculate derived property values from `staging_input`
    $staginginputpath = $_.staging_input.TrimStart("\")
    $staginginputcount = (Get-ChildItem -Recurse -File -Force -LiteralPath \?\UNC$staginginputpath | Measure-Object).Count
    $staginginputsize = Get-ChildItem -Recurse -File -Force -LiteralPath \?\UNC$staginginputpath | Measure-Object -property length -sum
    $staginginputsize = $staginginputsize.sum / 1KB

    # Calculate derived property values from `staging_location`
    $staginglocationpath = $_.staging_location.TrimStart("\")
    $staginglocationcount = (Get-ChildItem -Recurse -File -Force -LiteralPath \?\UNC$staginglocationpath | Measure-Object).Count
    $staginglocationsize = Get-ChildItem -Recurse -File -Force -LiteralPath \?\UNC$staginglocationpath | Measure-Object -property length -sum
    $staginglocationsize = $staginglocationsize.sum / 1KB

    # output new object with the property values calculated above
    [pscustomobject]@{
        staging_input = $_.staging_input
        staging_location = $_.staging_location
        staging_input_File_Count = $staginginputcount
        staging_location_File_Count = $staginglocationcount
        staging_input_File_Size = $staginginputsize
        staging_location_File_Size = $staginglocationsize
    }
} | Export-Csv $Finaloutput -NoTypeInformation