Retrieving VM inventory information using PowerCLI

Following on from collecting VM Host inventory information (http://wp.me/p15Mdc-mC), I am now looking into returning inventory information for all the VMs where the following would be retrieved:

  • Name
  • Host
  • vCPUs
  • Memory (MB)
  • CPU Reservation
  • CPU Limit
  • CPU Share Allocation
  • Memory Reservation
  • Memory Limt
  • Memory Share Allocation
  • Operating System
  • Annotations

In order to invoke the PowerCLI cmdlets  we will need to add the snap-ins to the current powershell session and connect to the vCenter server (in the below example this is named ‘server.domain.local’).

if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) 
   { 
   Add-PSSnapin VMware.VimAutomation.Core > $null
   } 

Connect-VIServer server.domain.local

Firstly, we will create an array to hold a list of items and build of a collection of VMs using the Get-VM cmdlet:

[array]$VMIs=@()
ForEach ($VM in Get-VM) 

For each VM returned in the collection we will create a new powershell object and store this as a variable to export the returned data to a CSV file and add properties to return the above information:

{
$VMI = New-Object PsObject
Add-Member -InputObject $VMI -MemberType NoteProperty -Name Name -Value $VM.Name
Add-Member -InputObject $VMI -MemberType NoteProperty -Name "Host" -Value $VM.Host
Add-Member -InputObject $VMI -MemberType NoteProperty -Name "vCPUs" -Value $VM.NumCPU
Add-Member -InputObject $VMI -MemberType NoteProperty -Name "Memory (MB)" -Value $VM.MemoryMB
Add-Member -InputObject $VMI -MemberType NoteProperty -Name "CPU Reservation" -Value $VM.ExtensionData.ResourceConfig.CpuAllocation.Reservation
Add-Member -InputObject $VMI -MemberType NoteProperty -Name "CPU Limit" -Value $VM.ExtensionData.ResourceConfig.CpuAllocation.Limit
Add-Member -InputObject $VMI -MemberType NoteProperty -Name "CPU Share Allocation" -Value $VM.ExtensionData.ResourceConfig.CpuAllocation.Shares.Level
Add-Member -InputObject $VMI -MemberType NoteProperty -Name "Memory Reservation" -Value $VM.ExtensionData.ResourceConfig.MemoryAllocation.Reservation
Add-Member -InputObject $VMI -MemberType NoteProperty -Name "Memory Limit" -Value $VM.ExtensionData.ResourceConfig.CpuAllocation.Limit
Add-Member -InputObject $VMI -MemberType NoteProperty -Name "Memory Share Allocation" -Value $VM.ExtensionData.ResourceConfig.MemoryAllocation.Shares.Level
Add-Member -InputObject $VMI -MemberType NoteProperty -Name "Operating System" -Value $VM.Guest.OSFullName

Part of my requirement is to return all the virtual machine type annotations, we will do this by returning all the available Annotations keys and create a column for each and display the data associated with that key.

ForEach ($CustomField in $VM.Customfields)
   {
   Add-Member -InputObject $VMI -MemberType NoteProperty -Name $CustomField.Key -Value $CustomField.Value
   }

Finally, we will export the data collected in the array into a CSV file, as  I want the output to create a unique filename each time the inventory script is invoked to the output folder D:\Output,  I will use the Guid.NewGuid method when specifying a filename.

$VMIs+=$VMI
 }
$VMIs | Export-Csv -NoTypeInformation -UseCulture -Path ("D:\Output\" + [guid]::NewGuid() + "-inv_vms.csv")

 

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s