2011-05-27

Simple VM inventory using PowerCLI

I needed a script that did a simple inventory of production VMs in a vSphere cluster. The output includes the VM name, its IP addresses (only if VMware Tools are installed), its network names, all .vmdk's and the notes field.

Here is the script:

# We go through all VMs that have a 'Production'-notes entry
$AllPrdVMs = Get-VM | Where {$_.Notes -like "Prod*"} | Sort Name
Foreach ($SingleVM in $AllPrdVMs)
{
$VMName    = $SingleVM.Name
# The following two lines enforce the use of an array.
$VMNetwork = @($SingleVM | Get-NetworkAdapter)
$VMHDDs    = @($SingleVM | Get-HardDisk)
# The following line only works if VMware Tools are installed
$VMIP      = $SingleVM.Guest.IPAddress
$VMNote    = $SingleVM.Notes
Write-Host ($VMName) -ForegroundColor Red -NoNewLine
Write-Host (" " + $VMIP) -ForegroundColor Yellow
# Here we loop through the arrays - even if there is only one member
For ($Count = 1; $Count -le $VMNetwork.Length; $Count++)
{
Write-Host $VMNetwork[$Count - 1].NetworkName
}
For ($Count = 1; $Count -le $VMHDDs.Length; $Count++)
{
Write-Host $VMHDDs[$Count - 1].Filename -ForegroundColor Blue
}
Write-Host ($VMNote)
Write-Host
}


PS: First post with syntax-highlighting ;-)

No comments:

Post a Comment