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")

 

Retrieving ESXi Host inventory information using PowerCLI

Over time I have produced a number of powershell scripts using the vSphere PowerCLI snap-in to return information from vCenter

Firstly, I looked into returning inventory information for all the ESXi Hosts where the following would be retrieved:

  • Name
  • Hardware Vendor
  • Hardware Model
  • CPU Model
  • Datacenter
  • Cluster
  • Hypervisor
  • Hypervisor Version
  • Clock Speed
  • Memory
  • Hyperthreading Active
  • Number of Cores

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

Now, in order to retrieve information for each ESXi host we will build a collection by invoking the Get-VMHost cmdlet

$VMHosts = Get-VMHost

From the ESXi hosts returned in the collection we will loop through each one and return the required information as above and return this as calculated properties and store in a variable to export later to a CSV file.

$Inventory = ForEach ($VMHost in $VMHosts)
   { 
   "" | Select-Object -Property @{N="Name";E={$VMHost.Name}},
   @{N="Vendor";E={(Get-View -ViewType HostSystem -Filter @{"Name" = $VMHost.Name}).Hardware.Systeminfo.Vendor}},
   @{N="Model";E={(Get-View -ViewType HostSystem -Filter @{"Name" = $VMHost.Name}).Hardware.Systeminfo.Model}},
   @{N="CPU Model";E={$VMHost.ExtensionData.Summary.Hardware.CpuModel}},
   @{N="Datacenter";E={(Get-Datacenter -VMHost $VMHost.Name).Name}},
   @{N="Cluster";E={(Get-Cluster -VMHost $VMHost.Name).Name}},
   @{N="Hypervisor";E={$VMHost.Extensiondata.Config.Product.Name}}, 
   @{N="Hypervisor Version";E={$VMHost.Extensiondata.Config.Product.Version}}, 
   @{N="Clock Speed (Mhz)";E={$VMHost.ExtensionData.Summary.Hardware.CpuMhz}},
   @{N="Memory (MB)";E={$VMHost.MemoryTotalMB}},
   @{N="Hyperthreading Enabled";E={$VMHost.HyperThreadingActive}},
   @{N="Number of Cores";E={$VMHost.ExtensionData.Summary.Hardware.numCpuCores}}
   }

As I want the output to create a unique filename each time the inventory script is invoked to the output folder D:\Output, therefore I will use the Guid.NewGuid method when specifying a filename.

$Inventory | Export-Csv -NoTypeInformation -Path ("D:\Output\" + [guid]::NewGuid() + "-inv_hosts.csv")

Below is a table to include the cmdlets required to extract the inventory information in the requirements.

Name Cmdlet Property
Name Get-VMHost Name
Hardware Vendor Get-View Hardware.Systeminfo.Vendor
Hardware Model Get-View Hardware.Systeminfo.Model
CPU Model Get-VMHost ExtensionData.Summary.Hardware.CpuModel
Datacenter Get-Datacenter Name
Cluster Get-Cluster Name
Hypervisor Get-VMHost ExtensionData.Config.Product.Name
Hypervisor Version Get-VMHost ExtensionData.Config.Product.Version
Clock Speed (Mhz) Get-VMHost ExtensionData.Summary.Hardware.CpuMhz
Memory (MB) Get-VMHost MemoryTotalMB
Hyperthreading Active Get-VMHost HyperThreadingActive
Number of Cores Get-VMHost ExtensionData.Summary.Hardware.numCpuCores

 

vSphere Inventory unable to connect to the remote server

I recently performed a search using the vSphere Client where I received the following warning message:

Image

Quite a simple one to resolve in this instance, I checked the service ‘vCenter Inventory Service’ which was in a stopped state, starting the service resolved this issue and allowed for the return of the inventory search result without restarting the vSphere client