2014-02-26 19:12:24 +00:00
|
|
|
Param(
|
|
|
|
[Parameter(Mandatory=$true)]
|
|
|
|
[string]$VmId
|
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) {
|
|
|
|
if ($network.IpAddresses.Length -gt 0) {
|
2017-07-28 17:18:26 +00:00
|
|
|
foreach ($ip_address in $network.IpAddresses) {
|
|
|
|
if ($ip_address.Contains(".")) {
|
|
|
|
$ip4_address = $ip_address
|
|
|
|
} elseif ($ip_address.Contains(":")) {
|
|
|
|
$ip6_address = $ip_address
|
|
|
|
}
|
|
|
|
if (-Not ([string]::IsNullOrEmpty($ip4_address)) -Or -Not ([string]::IsNullOrEmpty($ip6_address))) {
|
|
|
|
# We found our IP address!
|
|
|
|
break
|
|
|
|
}
|
2015-11-23 20:05:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 17:18:26 +00:00
|
|
|
if (-Not ([string]::IsNullOrEmpty($ip4_address))) {
|
|
|
|
$guest_ipaddress = $ip4_address
|
|
|
|
} elseif (-Not ([string]::IsNullOrEmpty($ip6_address))) {
|
|
|
|
$guest_ipaddress = $ip6_address
|
|
|
|
}
|
|
|
|
|
|
|
|
if (-Not ([string]::IsNullOrEmpty($guest_ipaddress))) {
|
|
|
|
$resultHash = @{
|
|
|
|
ip = $guest_ipaddress
|
|
|
|
}
|
|
|
|
$result = ConvertTo-Json $resultHash
|
|
|
|
Write-Output-Message $result
|
|
|
|
} else {
|
|
|
|
Write-Error-Message "Failed to determine IP address"
|
2014-02-15 23:29:16 +00:00
|
|
|
}
|