Search code examples
powershellcsvexport-to-csv

How to export csv of variables for each object loop


I have been stuck on trying to export in the format needed for this script for a month now. I can't figure out how to get it to export these variables in two separate loops into a single .csv file. We are given a .csv that contains staging_input and staging_location fields that both contain file locations on the network. We need to compare these two to make sure they are the same file count and size. I created a ForEach-Object loop for each of these fields and this gives me the desired output but I am unable to export-csv at the end of each loop into a single .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

Solution

  • Use a single loop instead of two:

    #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