How to create output csv with headers

Welcome Forums General PowerShell Q&A How to create output csv with headers

  • This topic has 2 replies, 2 voices, and was last updated 1 month ago by
    Participant
    .
Viewing 2 reply threads
  • Author
    Posts
    • #265859
      Participant
      Topics: 4
      Replies: 7
      Points: 67
      Rank: Member

      Hello,

      I am trying for format file system information from a VNX using PS with the plink command. I have the following code that returns the below output.

      $values = plink user@host -batch -i mycreds.ppk "export 'NAS_DB=/nas'; /nas/bin/nas_fs -query:inuse=y:type=uxfs:isroot=false -fields:Name,SizeValues -format:'%s,%s\n'"

      foreach($val in $values) {
          Write-Host $val
      }

      Cifs01_FS,3036900,361855,2675044,88,3109785976
      Cifs02_FS,5641353,1577535,4063818,72,5776746416
      Cifs03_FS,1970,1968,2,0,2018032

      The Write-Host command returns the correct output, but if I export to csv, I end up with just the lengths. I would like to export to csv correctly and to add some headers as well. It would also be great to assign names to each field to do some math and make it more legible.

      Any suggestions greatly appreciated.

    • #265877
      Participant
      Topics: 9
      Replies: 676
      Points: 2,674
      Helping Hand
      Rank: Community Hero

      You’re not showing how you export it. I’ll assume it includes your write-host. Drop the write host and just try this.

      I wasn’t sure what the values meant so just update the headers accordingly and this should work.

    • #265913
      Participant
      Topics: 4
      Replies: 7
      Points: 67
      Rank: Member

      You gave me the answer. It did not cross my mind to use the ConvertFrom-Csv. Once you made that suggestion, I was able to get what I wanted. Here is the final script:

      $OutTable = @()
      $values = plink <span class="crayon-i">user</span><span class="crayon-sy">@</span><span class="crayon-i">host</span><span class="crayon-h"> </span><span class="crayon-cn">-batch</span><span class="crayon-h"> </span><span class="crayon-cn">-i</span><span class="crayon-h"> </span><span class="crayon-i">mycreds</span><span class="crayon-sy">.</span><span class="crayon-i">ppk</span> "export 'NAS_DB=/nas'; /nas/bin/nas_fs -query:inuse=y:type=uxfs:isroot=false -fields:Name,SizeValues -format:'%s,%s\n'"
      $values | ConvertFrom-Csv -Header CIFS,Allocated,Consumed,Free,PCTFull,Blocks | 
        ForEach-Object {
          $ReptObj = New-Object PSObject
          $ReptObj | Add-Member -Type NoteProperty -Name "Array"       -Value "ArrayOne"   #Fixed value
          $ReptObj | Add-Member -Type NoteProperty -Name "CIFsName"    -Value $_.CIFS
          $ReptObj | Add-Member -Type NoteProperty -Name "AllocatedTB" -Value ([math]::Round($_.Allocated/1e+6,2))
          $ReptObj | Add-Member -Type NoteProperty -Name "ConsumedTB"  -Value ([math]::Round($i.Consumed/1e+6,2))
          $ReptObj | Add-Member -Type NoteProperty -Name "FreeTB"      -Value ([math]::Round($i.Free/1e+6,2))`&lt;/code&gt;<br />&lt;code&gt;    $ReptObj | Add-Member -Type NoteProperty -Name "PCTFull"     -Value $i.PCTFull`
          $OutTable += $ReptObj
        } 
      $OutTable | Export-Csv -Path C:\temp.csv -NoTypeInformation

      The output csv now has the headers and storage sizes converted to TBs.

      Your help is greatly appreciated.

       

      • This reply was modified 1 month ago by obijuan.
Viewing 2 reply threads
  • You must be logged in to reply to this topic.