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;
}
}
}