#PSTip How to identify Hyper-V virtual machines

Note: This tip requires PowerShell 2.0 or above.

There are several ways to test if a computer object is a virtual machine. Most, if not all, methods require a connection to each computer, usually by checking a WMI property. If the computer you are targeting is not available, this information will not be available as well.What if you wanted to get the information from a single location, such as Active Directory? When it comes to Hyper-V based VMs, you can.

When a Hyper-V virtual machine is added to Active Directory, a serviceConnectionPoint (SCP) object called “Windows Virtual Machine” is created under the computer object account (the object is created by the Hyper-V Integration service “Hyper-V Heartbeat Service”). We can use the “Windows Virtual Machine” object to identify Windows Hyper-V virtual machines.

PS> Get-ADObject -Filter {objectClass -eq 'serviceConnectionPoint' -and Name -eq 'Windows Virtual Machine'}

DistinguishedName                         Name                      ObjectClass             ObjectGUID
-----------------                         ----                      -----------             ----------
CN=Windows Virtual Machine,CN=server1... Windows Virtual Machine   serviceConnectionPoint  d4d935cf-1310-4b6b-967d-469a9da1a88d
CN=Windows Virtual Machine,CN=server2... Windows Virtual Machine   serviceConnectionPoint  e60feb40-794a-4237-99af-1b1ef33f2984
CN=Windows Virtual Machine,CN=server3... Windows Virtual Machine   serviceConnectionPoint  53107864-ae4f-478c-a76c-fc0675a6d1b8

Getting the virtual machine name is a bit tricky. You could parse the machine name from the DistinguishedName property but there’s an easier way. If you include the CanonicalName property you can quickly split it and get the name

Get-ADObject -Filter {objectClass -eq 'serviceConnectionPoint' -and Name -eq 'Windows Virtual Machine'} -Properties CanonicalName | ForEach-Object{

Filed in: Columns, Tips and Tricks Tags: , ,
© 2018 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: