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!
Best Answer
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 useGet-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 version10.0.x
.