vCenter Server automated Inventory with PowerCLI

As you can see in my previous articles I created and filled in the Custom Attributes fields. The aim of the whole customization to store all data in the vCenter Server, easily do an automated inventory. The script not needs an SAP connection to gather the required information, because all data available in one information store.

Here is my inventory script, I wrote in PowerCLI, and it creates a CSV file. The whole inventory script works without any user intervention, so you can easily add to a scheduled task. Each part commented, in case of better understanding.

#connect to vCenter Server
$VIConnection = Connect-VIServer testvCenter  –user “svc_inventory” –Pass “Asdf1234” –Protocol Https
#if any error happens during connection, the script will be stop
if (-not $VIConnection.IsConnected) {
Write-Host "Unable to connect to vCenter, please ensure you have altered the vCenter server address correctly”
Write-Host " to specify a username and password edit the connection string "

#set the ouput file
$CurrentDate = get-date -uformat "%Y%m%d"
$ReportFile = $CurrentDate+'_Resources.csv'

#user info for bughunting
Write-Host -ForegroundColor Yellow "   Generated file: $ReportFile"

#write the CSV file header
echo "VMName;HostID;SAPnumber;Owner;vCPU;vRAM(GB);Cluster" > $ReportFile

#get all VM
$vms = Get-VM
$vmsCount = $vms.length

#process all VM
$i = 0
for ($i=0; $i -lt $vmsCount; $i++)
#write progress in debugging mode
$progress = (($i / $vmsCount)  * 100 -as [int]).tostring() + '%'
Write-Host -ForegroundColor White `r 'Writing VMs  ' $progress  -NoNewline
#get VM name
$VMName = $vms[$i].Name
#get CPU count
$vCPU = $vms[$i].NumCpu
#get memory info
$vRAM = $vms[$i].MemoryMB / 1024
#read VM Custom Attributes
$vmsCust = $vms[$i].CustomFields
#get VM's cluster
$Cluster = (Get-Cluster -vm $vms[$i]).Name
#search into the Custom Attributes
foreach ($j in $vmsCust)
if ( $j.Key -eq 'HostID' ) { $HostID = $j.Value }
if ( $j.Key -eq 'SAPnumber' ) { $SAPnumber = $j.Value }
if ( $j.Key -eq 'Owner' ) { $Owner = $j.Value }
echo "$VMName;$HostID;$SAPnumber;$Owner;$vCPU;$vRAM;$Cluster" >> $ReportFile
#correcting progress bar
If ($i -eq $vmsCount)
Write-Host -ForegroundColor White `r 'Writing VMs  100%'  -NoNewline
Write-Host -ForegroundColor Yellow "CSV file generated successfully!"
#disconnecting from vCenter Server
Disconnect-VIServer -Confirm:$False

At the beginning I used the “$vRAM = $vms[$i].MemoryGB”, but I found a bug, so you can find this “$vRAM = $vms[$i].MemoryMB / 1024” in the final version of the script. You can read about the bug here.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s