Using PowerShell to Query WMI to Locate vSphere and AHV VMs!
Have you ever needed to quickly and easily locate all of the VMs in your domain? Recently I was involved in helping out some colleagues to determine which machines in a domain are either vSphere or AHV (Nutanix) based VMs. Microsoft makes it easy to locate Hyper-V based VMs with a simple dsquery command:
dsquery * Domainroot –Filter "(&(objectClass=serviceConnectionPoint)(CN=Windows Virtual Machine))"
However, vSphere and AHV based machines are not quite this easy, so lets first examine an individual computer object.
From a command prompt run:
C:UsersUserID>systeminfo
The results illustrate the System Manufacturer and System Model:
Image 001 – vSphere Virtual Machine
Image 002 – AHV (Nutanix) VM
With this information we can now utilize a few lines of simple PowerShell to query WMI on the local computer accounts to easily determine which objects are vSphere or AHV VMs.
$strcomputer = "COMPUTERNAME" $output = Get-wmiobject -Class Win32_ComputerSystem -ComputerName $strcomputer $output
If we take this one step further it is very easy loop through all objects within Active Directory and have the output sent to a .CSV file for further usage afterwards.
Remove-Item c:tempOutput.csv -EA SilentlyContinue $Computers = Get-ADComputer -Filter * | Sort-Object -Property Name ForEach ($Computer in $Computers) { Write-Host "Checking $($Computer.Name)" If (Test-Connection -ComputerName $Computer.Name -Quiet -Count 1) { $Object = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $Computer.name -EA SilentlyContinue | Where Model -eq "VMware Virtual Platform" | Select-Object Name | Export-Csv -Append -NoTypeInformation C:tempOutput.csv #$Object | Format-Table -Property * -AutoSize } }
This example searches for the Model = “VMware Virtual Platform” – If you needed to search for all AHV based VMs just replace the search criteria with AHV. If you wanted to search for all Dell or HP model servers this PowerShell can be utilized too!
Wrap-Up
PowerShell to the rescue once again as we query WMI on individual computer objects to determine the Manufacturer and Model for VMware vSphere and Nutanix AHV! I hope you find this article to be useful.