1

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
  • 113
  • 1
  • 5
  • Besides 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. – SamuelWarren Sep 20 '17 at 17:27
  • Have you tried probing Hyper-V for details that might lead you to figure out the guest OS version? – GregL Sep 20 '17 at 17:28
  • I've looked through the object you get back from Get-VM, but I didn't see anything there. – SamuelWarren Sep 20 '17 at 18:02
  • Can't you use `Get-WMIObject -Class Win32_OperatingSystem` to find out what OS it's running then go from there? – shinjijai Sep 20 '17 at 18:08
  • I'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). – SamuelWarren Sep 20 '17 at 18:13
  • Use `Get-WMIObject -Class Win32_OperatingSystem -ComputerName $VMName -Credential $VMCredentials`. You can replace the $VMName with an IP address if you want as well. – shinjijai Sep 20 '17 at 18:14
  • That gives a "No Such Interface Supported" error. It works locally, just not with a ComputerName attribute. – SamuelWarren Sep 20 '17 at 18:39
  • Are RPC services running on those servers? – shinjijai Sep 20 '17 at 18:42
  • @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](https://social.technet.microsoft.com/wiki/contents/articles/33796.hyper-v-integration-services-list-of-build-numbers.aspx) 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. – GregL Sep 20 '17 at 18:52

1 Answers1

2

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.

shinjijai
  • 416
  • 1
  • 7
  • 16
  • I'm getting an RPC error... Which I think means this will work. I'll let you know. – SamuelWarren Sep 20 '17 at 19:00
  • Ok I got my RPC Server working correctly. This works like a charm! – SamuelWarren Sep 22 '17 at 14:35
  • There 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. – fcm Nov 22 '21 at 21:12