2014-02-26 19:12:24 +00:00
|
|
|
Param(
|
|
|
|
[Parameter(Mandatory=$true)]
|
|
|
|
[string]$VmId
|
2018-04-26 19:53:52 +00:00
|
|
|
)
|
2014-02-15 23:29:16 +00:00
|
|
|
|
|
|
|
# Include the following modules
|
2014-02-26 19:12:24 +00:00
|
|
|
$Dir = Split-Path $script:MyInvocation.MyCommand.Path
|
|
|
|
. ([System.IO.Path]::Combine($Dir, "utils\write_messages.ps1"))
|
2014-02-15 23:29:16 +00:00
|
|
|
|
2017-08-08 17:00:29 +00:00
|
|
|
$vm = Hyper-V\Get-VM -Id $VmId -ErrorAction "Stop"
|
|
|
|
$networks = Hyper-V\Get-VMNetworkAdapter -VM $vm
|
2015-11-23 20:05:37 +00:00
|
|
|
foreach ($network in $networks) {
|
2018-04-26 19:53:52 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# 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 | select IPAddress
|
|
|
|
$ip_address = $addr.IPAddress
|
|
|
|
if ($ip_address.Contains(".")) {
|
|
|
|
$ip4_address = $ip_address
|
|
|
|
} elseif ($ip_address.Contains(":")) {
|
|
|
|
$ip6_address = $ip_address
|
|
|
|
}
|
2015-11-23 20:05:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 17:18:26 +00:00
|
|
|
if (-Not ([string]::IsNullOrEmpty($ip4_address))) {
|
2018-04-26 19:53:52 +00:00
|
|
|
$guest_ipaddress = $ip4_address
|
2017-07-28 17:18:26 +00:00
|
|
|
} elseif (-Not ([string]::IsNullOrEmpty($ip6_address))) {
|
2018-04-26 19:53:52 +00:00
|
|
|
$guest_ipaddress = $ip6_address
|
2017-07-28 17:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (-Not ([string]::IsNullOrEmpty($guest_ipaddress))) {
|
2018-04-26 19:53:52 +00:00
|
|
|
$resultHash = @{
|
|
|
|
ip = $guest_ipaddress
|
|
|
|
}
|
|
|
|
$result = ConvertTo-Json $resultHash
|
|
|
|
Write-Output-Message $result
|
2017-07-28 17:18:26 +00:00
|
|
|
} else {
|
2018-04-26 19:53:52 +00:00
|
|
|
Write-Error-Message "Failed to determine IP address"
|
2014-02-15 23:29:16 +00:00
|
|
|
}
|