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!