3

I wrote this script to output a table formatted file:

$VMs = Get-AzureRmVM
$vmOutput = $VMs | ForEach-Object { 
    [PSCustomObject]@{
        "VM Name" = $_.Name
        "VM Type" = $_.StorageProfile.osDisk.osType
        "VM Profile" = $_.HardwareProfile.VmSize
        "VM OS Disk Size" = $_.StorageProfile.OsDisk.DiskSizeGB
        "VM Data Disk Size" = ($_.StorageProfile.DataDisks.DiskSizeGB) -join ','
    }
}

$vmOutput  | export-csv C:\***\data.csv -delimiter ";" -force -notypeinformation 

Is there any possibility to sum "VM Data Disk Size" for each object? I used -join parameter only because data wasn't properly exported, because of more than one data disk per vm.

lubierzca
  • 160
  • 2
  • 14

1 Answers1

4

if you have an array of numbers and you want to sum them use:

($NumArray | Measure -Sum).Sum

in your case it would be:

"VM Data Disk Size" = ($_.StorageProfile.DataDisks.DiskSizeGB | Measure -Sum).Sum

Measure is an alias for Measure-Object if you're looking for the help pages or anything, it can also do Average, Min/Max etc.

colsw
  • 3,216
  • 1
  • 14
  • 28
  • @lubierzca no problem, just as an FYI `Measure` has some minor performance impact per use (as in custom functions could be faster) but shouldn't be noticeable unless you're iterating multiple thousand times. – colsw Jul 04 '18 at 11:10
  • Is it possible to also count amount of disks? I didn't found a Count parameter in Measure-Objet – lubierzca Jul 04 '18 at 13:44
  • 1
    @lubierzca you can just do `( ... | Measure ).Count`, there's no `-Count` required, you only need to Specify Sum or Average or Min/Max if you actually want those results on top. – colsw Jul 04 '18 at 15:07