2018-05-24 16:57:55 +00:00
|
|
|
#Requires -Modules VagrantMessages
|
|
|
|
|
|
|
|
param(
|
2014-02-26 19:12:24 +00:00
|
|
|
[Parameter(Mandatory=$true)]
|
|
|
|
[string]$VmId
|
2018-04-26 19:53:52 +00:00
|
|
|
)
|
2014-02-15 23:29:16 +00:00
|
|
|
|
2018-05-24 16:57:55 +00:00
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
|
|
|
|
try {
|
|
|
|
$vm = Hyper-V\Get-VM -Id $VmId
|
|
|
|
} catch {
|
2018-06-12 22:32:29 +00:00
|
|
|
Write-ErrorMessage "Failed to locate VM: ${PSItem}"
|
2018-05-24 16:57:55 +00:00
|
|
|
exit 1
|
|
|
|
}
|
2014-02-15 23:29:16 +00:00
|
|
|
|
2018-05-24 16:57:55 +00:00
|
|
|
try {
|
|
|
|
$networks = Hyper-V\Get-VMNetworkAdapter -VM $vm
|
|
|
|
foreach ($network in $networks) {
|
|
|
|
if ($network.IpAddresses.Length -gt 0) {
|
|
|
|
foreach ($ip_address in $network.IpAddresses) {
|
|
|
|
if ($ip_address.Contains(".") -And [string]::IsNullOrEmpty($ip4_address)) {
|
|
|
|
$ip4_address = $ip_address
|
|
|
|
} elseif ($ip_address.Contains(":") -And [string]::IsNullOrEmpty($ip6_address)) {
|
|
|
|
$ip6_address = $ip_address
|
|
|
|
}
|
2018-04-26 19:53:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-24 16:57:55 +00:00
|
|
|
# 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
|
|
|
|
}
|
2018-05-01 00:39:27 +00:00
|
|
|
}
|
2018-04-26 19:53:52 +00:00
|
|
|
}
|
2015-11-23 20:05:37 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 16:57:55 +00:00
|
|
|
if (-Not ([string]::IsNullOrEmpty($ip4_address))) {
|
|
|
|
$guest_ipaddress = $ip4_address
|
|
|
|
} elseif (-Not ([string]::IsNullOrEmpty($ip6_address))) {
|
|
|
|
$guest_ipaddress = $ip6_address
|
|
|
|
}
|
2017-07-28 17:18:26 +00:00
|
|
|
|
2018-05-24 16:57:55 +00:00
|
|
|
if (-Not ([string]::IsNullOrEmpty($guest_ipaddress))) {
|
|
|
|
$resultHash = @{
|
|
|
|
ip = $guest_ipaddress
|
|
|
|
}
|
|
|
|
$result = ConvertTo-Json $resultHash
|
2018-06-12 22:32:29 +00:00
|
|
|
Write-OutputMessage $result
|
2018-05-24 16:57:55 +00:00
|
|
|
} else {
|
2018-06-12 22:32:29 +00:00
|
|
|
Write-ErrorMessage "Failed to determine IP address"
|
2018-05-24 16:57:55 +00:00
|
|
|
exit 1
|
2018-04-26 19:53:52 +00:00
|
|
|
}
|
2018-05-24 16:57:55 +00:00
|
|
|
} catch {
|
2018-06-12 22:32:29 +00:00
|
|
|
Write-ErrorMessage "Unexpected error while detecting network configuration: ${PSItem}"
|
2018-05-24 16:57:55 +00:00
|
|
|
exit 1
|
2014-02-15 23:29:16 +00:00
|
|
|
}
|