How to create output csv with headers

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.

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

$values | ConvertFrom-Csv -Header Header1,Header2,Header3,Header4,Header5,Header6 |
    Export-Csv -Path C:\Some\path\to.csv -NoTypeInformation

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

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 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’”
$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))</code> <code> $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.