How to tell what Operating System is running in HyperV VM with Powershell
Unfotunately Get-VM
cmdlet does not give you any details on what OS the guest VM is running. Since you're trying to find out if the guest OS is either Windows Server 2016 or Windows Server 2012, you can use Get-WMIObject
to retrieve that exact information.
Using the following command should bring you back the version number:
Get-WMIObject -Class Win32_OperatingSystem -ComputerName $VMName -Credential $VMCredentials | Select-Object *Version -ExpandProperty Version*
Windows 2012 is version 6.3.x
while Windows 2016 is version 10.0.x
.
Related videos on Youtube
SamuelWarren
Solution Architect in multiple technologies currently working a nice job at a good shop.I have your answers.
Updated on September 18, 2022Comments
-
SamuelWarren over 1 year
So here's the environment.
Host: Windows 10
Guests: Server 2012 and Server 2016
Purpose: Development system to automate pushing out builds to servers for testing during development.I've got a Powershell function as part of this full build script that gets a remote PSSession for the VM I'm going to be working with. Since the VM is cloned dynamically from a base system, at this point I don't know what the OS in the VM is.
If it's a 2016 VM, I'd rather use Powershell Direct to connect to the VM.
$session = New-PSSession -VMName $VMName -Credential $VMCredentials
If it's 2012 I have to fall back to a WinRM session across the network. I've got the code to get the IP and make the connection. It works fine.
$vm = Get-Vm -Name $VMName $ips = New-Object System.Collections.Generic.List[System.String] foreach ($adapter in $vm.NetworkAdapters) { foreach ($ip in $adapter.IPAddresses) { if($ip -like '*.*') { $ips.Add($ip) } } } $session = New-PSSession -ComputerName $ips[0] -Authentication Negotiate -Credential $VMCredentials
What I need is to know how to tell whether I should be running the Powershell Direct (2016 only) or the WinRM network based connection (2012 and older).
My thought is the Hyper-V cmdlets have to have some way to tell what OS is IN the VM. Maybe not. I'm open to other ways of solving this too. Thanks!
-
SamuelWarren over 6 yearsBesides googleing for soltuions, I tried a Try Catch block around the first method with the catch trying the second method. This still sent back an error from the cmdlet, so I tried -ErrorAction Ignore on the first one, and if $session was -eq $null, then use the second block. It still fails to run the second block.
-
GregL over 6 yearsHave you tried probing Hyper-V for details that might lead you to figure out the guest OS version?
-
SamuelWarren over 6 yearsI've looked through the object you get back from Get-VM, but I didn't see anything there.
-
SamuelWarren over 6 yearsI'm running this code on the Host not the Guest. This is the code that gets me the remote connection where I could run that (unless there's a way to use the WMI objects against a remote system).
-
shinjijai over 6 yearsUse
Get-WMIObject -Class Win32_OperatingSystem -ComputerName $VMName -Credential $VMCredentials
. You can replace the $VMName with an IP address if you want as well. -
SamuelWarren over 6 yearsThat gives a "No Such Interface Supported" error. It works locally, just not with a ComputerName attribute.
-
shinjijai over 6 yearsAre RPC services running on those servers?
-
GregL over 6 years@SamuelWarren How about the
IntegrationServicesVersion
property? The version of the IS should be a dead give away as to the VM's OS, as per this article. Not sure what the versions would be for 2016, but I'd think it will be 10.0.x, based on the OS build numbers.
-
-
SamuelWarren over 6 yearsI'm getting an RPC error... Which I think means this will work. I'll let you know.
-
SamuelWarren over 6 yearsOk I got my RPC Server working correctly. This works like a charm!
-
fcm over 2 yearsThere is another issue. You assume the VMName coming from the Get-VM is the same than the guest computer DNS name... This is not always the case, I'm trying to find if the virtual machine is Linux but with a 'random' name.