Quick VMWare inventory script

 

here is a little script i use to gather few informations from our VMWare farm. This snippet goal is to retrieve informations about CPU, Cores and disks in a variable in order to do some filtering on informations, to brainstorm about new solutions, specially storage solution.

I’ve find many scripts on the internet, but none of them where sexy in my opinion… So i decided to make mine and share it with you.

$Output = @()
Get-View -SearchRoot (Get-Cluster '<datacenter>' | Get-View).MoRef -ViewType virtualmachine -Filter @{'Config.Template'='False'} | Select *, @{N="Disks";E={@($_.Guest.Disk.Length)}} | Sort-Object -Descending Disks | % {
    $VM = $_
    $prop = New-Object System.Collections.Specialized.OrderedDictionary
    $prop.add('Name',$VM.name)
    $prop.add('CPUSocket',$vm.config.hardware.NumCPU)
    $prop.add('CorePerSocket',$vm.config.hardware.NumCoresPerSocket)
    $Disk = 0
    $TotalSize = 0
    $VM.Guest.Disk | % {
        $prop.add("Disk$($Disk) Letter/Path",$_.DiskPath)
        $prop.add("Disk$($Disk) Capacity (GB)",[math]::Round($_.Capacity/ 1GB))
        $prop.add("Disk$($Disk) FreeSpace (GB)",[math]::Round($_.FreeSpace / 1GB))
        $TotalSize = $TotalSize + [math]::Round($_.Capacity/ 1GB)
        $Disk++
    }
    $prop.add('TotalSize',$TotalSize)
    $Output += [PSCustomObject]$prop
}

$Output

You can find the script on Github.

Update: Thanks to Grzegorz Kulikowski hint, i’ve changed the line 2 to gain much more speed, and sexy šŸ˜€