Merge pull request #9737 from chrisroberts/e-hyper-v-addr

Check neighbors for valid guest address when default lookup fails
This commit is contained in:
Chris Roberts 2018-05-02 08:54:57 -07:00 committed by GitHub
commit fbcd86993b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 35 additions and 21 deletions

View File

@ -1,7 +1,7 @@
Param( Param(
[Parameter(Mandatory=$true)] [Parameter(Mandatory=$true)]
[string]$VmId [string]$VmId
) )
# Include the following modules # Include the following modules
$Dir = Split-Path $script:MyInvocation.MyCommand.Path $Dir = Split-Path $script:MyInvocation.MyCommand.Path
@ -10,33 +10,47 @@ $Dir = Split-Path $script:MyInvocation.MyCommand.Path
$vm = Hyper-V\Get-VM -Id $VmId -ErrorAction "Stop" $vm = Hyper-V\Get-VM -Id $VmId -ErrorAction "Stop"
$networks = Hyper-V\Get-VMNetworkAdapter -VM $vm $networks = Hyper-V\Get-VMNetworkAdapter -VM $vm
foreach ($network in $networks) { foreach ($network in $networks) {
if ($network.IpAddresses.Length -gt 0) { if ($network.IpAddresses.Length -gt 0) {
foreach ($ip_address in $network.IpAddresses) { foreach ($ip_address in $network.IpAddresses) {
if ($ip_address.Contains(".")) { if ($ip_address.Contains(".") -And [string]::IsNullOrEmpty($ip4_address)) {
$ip4_address = $ip_address $ip4_address = $ip_address
} elseif ($ip_address.Contains(":")) { } elseif ($ip_address.Contains(":") -And [string]::IsNullOrEmpty($ip6_address)) {
$ip6_address = $ip_address $ip6_address = $ip_address
} }
if (-Not ([string]::IsNullOrEmpty($ip4_address)) -Or -Not ([string]::IsNullOrEmpty($ip6_address))) { }
# We found our IP address! }
break }
}
# If no address was found in the network settings, check for
# neighbor with mac address and see if an IP exists
if (([string]::IsNullOrEmpty($ip4_address)) -And ([string]::IsNullOrEmpty($ip6_address))) {
$macaddresses = $vm | select -ExpandProperty NetworkAdapters | select MacAddress
foreach ($macaddr in $macaddresses) {
$macaddress = $macaddr.MacAddress -replace '(.{2})(?!$)', '${1}-'
$addr = Get-NetNeighbor -LinkLayerAddress $macaddress -ErrorAction SilentlyContinue | select IPAddress
if ($ip_address) {
$ip_address = $addr.IPAddress
if ($ip_address.Contains(".")) {
$ip4_address = $ip_address
} elseif ($ip_address.Contains(":")) {
$ip6_address = $ip_address
}
}
} }
}
} }
if (-Not ([string]::IsNullOrEmpty($ip4_address))) { if (-Not ([string]::IsNullOrEmpty($ip4_address))) {
$guest_ipaddress = $ip4_address $guest_ipaddress = $ip4_address
} elseif (-Not ([string]::IsNullOrEmpty($ip6_address))) { } elseif (-Not ([string]::IsNullOrEmpty($ip6_address))) {
$guest_ipaddress = $ip6_address $guest_ipaddress = $ip6_address
} }
if (-Not ([string]::IsNullOrEmpty($guest_ipaddress))) { if (-Not ([string]::IsNullOrEmpty($guest_ipaddress))) {
$resultHash = @{ $resultHash = @{
ip = $guest_ipaddress ip = $guest_ipaddress
} }
$result = ConvertTo-Json $resultHash $result = ConvertTo-Json $resultHash
Write-Output-Message $result Write-Output-Message $result
} else { } else {
Write-Error-Message "Failed to determine IP address" Write-Error-Message "Failed to determine IP address"
} }