Check if hyper-v addresses is an array before slicing it

This commit is contained in:
Jordan Borean 2018-06-18 14:45:33 +10:00
parent dcb870bcbf
commit efa9021974
No known key found for this signature in database
GPG Key ID: 2AAC89085FBBDAB5
1 changed files with 5 additions and 1 deletions

View File

@ -1009,7 +1009,11 @@ param([string]$mac, [int]$addressIndex)
try { try {
$vm = Hyper-V\Get-VM | ?{$_.NetworkAdapters.MacAddress -eq $mac} $vm = Hyper-V\Get-VM | ?{$_.NetworkAdapters.MacAddress -eq $mac}
if ($vm.NetworkAdapters.IpAddresses) { if ($vm.NetworkAdapters.IpAddresses) {
$ip = $vm.NetworkAdapters.IpAddresses[$addressIndex] $ipAddresses = $vm.NetworkAdapters.IPAddresses
if ($ipAddresses -isnot [array]) {
$ipAddresses = @($ipAddresses)
}
$ip = $ipAddresses[$addressIndex]
} else { } else {
$vm_info = Get-CimInstance -ClassName Msvm_ComputerSystem -Namespace root\virtualization\v2 -Filter "ElementName='$($vm.Name)'" $vm_info = Get-CimInstance -ClassName Msvm_ComputerSystem -Namespace root\virtualization\v2 -Filter "ElementName='$($vm.Name)'"
$ip_details = (Get-CimAssociatedInstance -InputObject $vm_info -ResultClassName Msvm_KvpExchangeComponent).GuestIntrinsicExchangeItems | %{ [xml]$_ } | ?{ $_.SelectSingleNode("/INSTANCE/PROPERTY[@NAME='Name']/VALUE[child::text()='NetworkAddressIPv4']") } $ip_details = (Get-CimAssociatedInstance -InputObject $vm_info -ResultClassName Msvm_KvpExchangeComponent).GuestIntrinsicExchangeItems | %{ [xml]$_ } | ?{ $_.SelectSingleNode("/INSTANCE/PROPERTY[@NAME='Name']/VALUE[child::text()='NetworkAddressIPv4']") }