Powershell VM Configuration Report

One of the things I like to do is produce reports for management through powershell scripts.  The simplest way  to produce a quick report is through a collection of one liners encased in foreach loops.

We require a way to produce a Quality Assurance Check on many occasions for us to verify our work and also to output for the business user.

Where to start?  Look at what information you are wanting to output, start with the easiest and work your way to the harder.  Easy would be regular configuration items: Number of CPUs, Amount of RAM, getting the annotation or notes from the VM,etc...

Start with connecting to the vCenter.

connect-viserver <vCenter> -alllinked 

#you can use connect-viserver or the alias get-esx
#the alllinked variable connects to all vCenter's that are in Linked Mode

Get the Inputs (VM Name, report output filename, etc...)

$vmname = read-host "VM Name to Query"

$filename = ".\output" + $vmname + "_QA.txt"

Figure the Outputs (# of vCPUs, amount of RAM, Network)

Get-VM $vmname | Select Name, NumCPU, MemoryMB
Get-VM $vmname | get-NetworkAdapter| Format-Table -autosize -property Parent, Name, NetworkName, MacAddress, Type

Figure out more complex Outputs (IPaddress via the VMtools)

$vmview = get-vm $vmname | get-view

foreach ($item in $vmview.guest.net){
    $IP = $Item | %{$_.IPAddress}
    'IP Address: ' | $IP
    $nmb = $Item.IPConfig | %{$_.IPAddress} | %{$_.PrefixLength}
    'Netmask: ' + $nmb
    }

Or figure out the SCSI Bus information

Foreach ($vSCSICntl in ($vmview.config.hardware.device | where {$_.DeviceInfo.Label -match "SCSI Controller"})){
    'Controller: ' + $vSCSICntl | %{$_.UnitNumber}
    Foreach ($vDisk in ($vmview.Config.Hardware.Device | where    ($_.ControllerKey -eq $vSCSICntl})){
       $Label = $vDisk.DeviceInfo | %{$_.Label}
       $BusNum = $vSCSICntl | %{$_.BusNumber}
       $DiskDev = $vDisk | %{$_.UnitNumber}
       'Disk: ' + $Label
       'SCSI Bus: ' + $BusNum + ":" + $DiskDev
       }
    }

Put in your comments if you can think of additional report items.

Halchris




Comments