Condividi tramite


How to get the IP address of a virtual machine from Hyper-V

With the release of Windows Server 2008 R2, Hyper-V added the capability to get the IP address assigned to a Virtual Machine from the Host KVP items. In order to get this IP address you need the latest integration services installed inside the virtual machine.

FYI, VMM does not currently expose these IP Addresses, but this is something that you will see in VMM in the future for sure.

To get these IP Addresses and other KVP items follow these steps.

Open notepad and copy the contents below into a filed called Get-HyperVKVP.ps1. You can simply edit the value of the $vm and run this PowerShell script as an administrator locally on the Hyper-V server. It will give you all the KVP's associated with the virtual machine, including the IP Address. See below for an example output.

<<

$vm = "<enter the friendly name of a virtual machine";

filter Import-CimXml
{
    $CimXml = [Xml]$_
    $CimObj = New-Object -TypeName System.Object
    foreach ($CimProperty in $CimXml.SelectNodes("/INSTANCE/PROPERTY"))
    {
 if ($CimProperty.Name -eq "Name" -or $CimProperty.Name -eq "Data")
 {
         $CimObj | Add-Member -MemberType NoteProperty -Name $CimProperty.NAME -Value $CimProperty.VALUE
 }
    }
    $CimObj
}

$VmObj = Get-WmiObject -Namespace root\virtualization -Query "Select * From Msvm_ComputerSystem Where ElementName='$vm'"
$KvpObj = Get-WmiObject -Namespace root\virtualization -Query "Associators of {$VmObj} Where AssocClass=Msvm_SystemDevice ResultClass=Msvm_KvpExchangeComponent"

$KvpObj.GuestIntrinsicExchangeItems | Import-CimXml

>>

PS C:\Users\Admin\Desktop>  .\Get-HyperVKVP.ps1

Data                                                        Name
----                                                        ----
WIN-D8R7D2893P8                                             FullyQualifiedDomainName
Windows Server 2008 R2 Enterprise                           OSName
6.1.7600                                                    OSVersion
                                                            CSDVersion
6                                                           OSMajorVersion
1                                                           OSMinorVersion
7600                                                        OSBuildNumber
2                                                           OSPlatformId
0                                                           ServicePackMajor
0                                                           ServicePackMinor
274                                                         SuiteMask
3                                                           ProductType
10                                                          OSEditionId
9                                                           ProcessorArchitecture
6.1.7600.16385                                              IntegrationServicesVersion
169.254.159.251;172.30.169.167 NetworkAddressIPv4
fe80::c188:6b14:d667:9ffb%18;2001:4898:2c:3:c9fd:e7ae:f2... NetworkAddressIPv6
                                                            RDPAddressIPv4
                                                            RDPAddressIPv6