Condividi tramite


Retrieving the IP Address Of A VM In Hyper-V

This post is to update an old from one 2008 Hyper-V WMI Using PowerShell Scripts – Part 3 (KVP's - Guest OS Version) – in reviewing that post the most coming inquiry was looking for the guests IP address, well we got that for you in PowerShell now…  With the introduction of Windows Server 2012 Hyper-V now includes 164 different PowerShell cmdlet’s.  On of those is Get-VMNetworkAdapter and one of the properties we included in that cmdlet is the IPAddress that network adapter has assigned to it.  We retrieve that information using the tried and true key value pair integration component.

PowerShell Example

PS C:\> (Get-VMNetworkAdapter -VMName VM1).IpAddresses
192.168.0.104
fe80::3114:f7d4:4561:9ea2
2001:1234:a:2:7891:f7d4:4561:9ea2

WMI Example Querying The Key Value Pair (KVP) Integration Component

$vm = Get-WmiObject -Namespace root\virtualization\v2 -Class `
Msvm_ComputerSystem -Filter {ElementName='VM1'}

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

Other Key Value Pair’s Provided

KVP Name Sample Value
FullyQualifiedDomainName vm1.contoso.com
OSName Windows Server 2008 R2 Enterprise
OSVersion 6.1.7601
CSDVersion Service Pack 1
OSMajorVersion 6
OSMinorVersion  1
OSBuildNumber 7601
OSPlatformId 2
ServicePackMajor 1
ServicePackMinor 0
SuiteMask 274
ProductType 3
OSVendor 1
OSSignature 4
OSEditionId 10
ProcessorArchitecture 9
IntegrationServicesVersion 6.2.9200.16433
NetworkAddressIPv4 192.168.0.123
NetworkAddressIPv6 fe80::3114:f7d4:4561:9ea2;2001:1234:a:2:7891:f7d4:4561:9ea2
RDPAddressIPv4 192.168.0.123
RDPAddressIPv6 fe80::3114:f7d4:4561:9ea2;2001:1234:a:2:7891:f7d4:4561:9ea2

 

-taylorb

Comments

  • Anonymous
    February 24, 2014
    Your post helped me a lot .I was deadly stuck up on retrieving domainname of vm from hyper-v. Thanks!!

  • Anonymous
    July 24, 2014
    use this one (Get-VMNetworkAdapter -VMName <Name of the VM>).IpAddresses[0]