Microsoft Virtualization Discussions

How to output report in CSV?

NAMAN
3,599 Views

I'm running a below powershell script to report on our capacity but recently I was asked to provide it in CSV format.  

 

 

foreach ($filer in Get-Content \\[network share]\FILERS.txt) { $c = Connect-NaController -Name $filer; Write-Host "Controller: $filer"; Get-Naaggr | Format-Table -autosize -Property @{label="Aggr";Expression={$_.Name}}, State, @{label="TotalSize(TB)";expression={[math]::round($_.TotalSize / 1tb, 2)}}, @{label="Available";expression={[math]::round($_.Available / 1tb, 2)}}}, @{label="Used";expression={[math]::round($_.Available / 1tb, 2)}}}

 

 

 

Current report looks like this:


Controller: xxxxxx

Aggr State TotalSize(TB) Available
---- ----- ------------- ---------
aggr2 online 40.74       6.92
aggr3 online 20.37       16.72

 

 

 

How can I modify the script to output in CSV?  Can I have it output in the fields as follows?

 

ReportDate    Capacity   Free  FilerName   

 2015-10-14       xxxx        xxx      xxxxx

1 REPLY 1

Aparajita
3,584 Views

The cleanest (IMO) way is to create a custom object, and use Export-Csv to write the CSV file.

 

Something like this should do the trick (haven't executed this, so please excuse any syntax errors).

 

$date = Get-Date -Format yyyy-MM-dd

foreach ($filer in Get-Content \\[network share]\FILERS.txt) { 
  $c = Connect-NaController -Name $filer; 
  $aggrs = Get-Naaggr 
  foreach ($aggr in $aggrs)
  {
      $obj = [PSCustomObject]@{
        ReportDate = $date
        Capacity = $aggr.TotalSize #Convert to TB if required
        Free = $aggr.Available
        FilerName = $filer
      }
      $obj | Export-CSV -Path somePath -Append
  }
}

 

If Capacity & Free are to be reported per-filer, then

 

$date = Get-Date -Format yyyy-MM-dd

foreach ($filer in Get-Content \\[network share]\FILERS.txt) { 
  $c = Connect-NaController -Name $filer; 
  $sums = Get-Naaggr | Measure-Object -Sum Available,TotalSize
  $obj = [PSCustomObject]@{
        ReportDate = $date
        Capacity = $sums[1].Sum #Convert to TB if required
        Free = $sums[0].Sum
        FilerName = $filer
   }
   $obj | Export-CSV -Path somePath -Append
}

 

Hope this helps,

Aparajita

Public