3

I have created an automated build script to build new VMs on Hyper-V 2016. I sometimes need a 2008 R2 VM and the way I obtain the IP Address to connect to my 2012 R2/2016 VMs is to use some powershell like:

get-vm -Name $VMName|Get-VMNetworkAdapter|Select-Object -ExpandProperty IpAddresses

Works great for 2012/2016, but returns an empty array with a 2008 R2 VM. Any ideas on how to obtain the IP Address from Hyper-V with Powershell? The script is running from a Windows 10 workstation.

EDIT

I tried get-vm -name $VMName|Get-WmiObject -Class Win32_NetworkAdapterConfiguration as a test and got this after lots of adapter output:

Get-WmiObject : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.

SOLUTION

This is what I ended up doing:

$VMName = <the name of my vm>
Invoke-Command -Session $VMHostSession -ScriptBlock {
    $Vm = Get-WmiObject -Namespace root\virtualization\v2 -Query "Select * From Msvm_ComputerSystem Where ElementName='$using:VMName'";
    $vm.GetRelated("Msvm_KvpExchangeComponent").GuestIntrinsicExchangeItems | % { ` 
        $GuestExchangeItemXml = ([XML]$_).SelectSingleNode("/INSTANCE/PROPERTY[@NAME='Name']/VALUE[child::text()='NetworkAddressIPv4']"); 

        if ($GuestExchangeItemXml -ne $null) 
        { 
            $GuestExchangeItemXml.SelectSingleNode("/INSTANCE/PROPERTY[@NAME='Data']/VALUE/child::text()").Value; 
        }    
    }
}
Mark Allison
  • 2,188
  • 7
  • 26
  • 45

1 Answers1

0

You could use WMI for it. Maybe 2008R2 doesn't have the field you are looking for.

get-vm -Name $VMName | Get-WmiObject -Class Win32_NetworkAdapterConfiguration -computername $_ | select-object IPAddress

You may also need to do some filtering on the network card that you are choosing if you have multiples. You may also need to check what get-vm is outputting as I'm not sure it will pipe directly into get-wmiobject. You might need to pipe it to foreach-object and retrieve the computername that way.

****EDIT****

get-vm -Name $VMName | foreach-object { Get-WmiObject -Class Win32_NetworkAdapterConfiguration -computername $_.name } | select-object IPAddress

Hope this helps.

Thanks, Tim.

Tim Haintz
  • 486
  • 1
  • 3
  • 8
  • I only have one virtual NIC configured. When I run the command you suggested I get a lot of output, but none of the adapters have the correct IP address. See my edit for more info. – Mark Allison Jan 02 '17 at 23:33
  • I don't have HyperV to test at the moment. What property is the server name in? I will edit the above answer and you should just be able to add the correct property. I have used $_.name in my edited example. If you just run the get-wmiobject -class Win32_NetworkAdapterConfiguration on the 2008 server, does that have the correct IP address info? – Tim Haintz Jan 03 '17 at 00:02
  • Running this inside the VM returns the IP Address: `(Get-WMIObject -Class Win32_NetworkAdapterConfiguration | ? {$_.DHCPEnabled -eq $true}).IpAddress[0]`. But running on the Hyper-V host piping from Get-VM does not return the IP. – Mark Allison Jan 13 '17 at 17:24
  • Solved using code from here https://blogs.msdn.microsoft.com/taylorb/2008/05/06/hyper-v-wmi-using-powershell-scripts-part-3-kvps-guest-os-version/ – Mark Allison Jan 13 '17 at 19:19