From d4ff29f6c5cb050dad50de0c99efb4a83a2af9ab Mon Sep 17 00:00:00 2001 From: Marcin Bojko Date: Thu, 22 Aug 2019 07:54:50 +0200 Subject: [PATCH] hyper-v fix when management interface is not part of virtual switch 2 formatting fix --- common/powershell/hyperv/hyperv.go | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/common/powershell/hyperv/hyperv.go b/common/powershell/hyperv/hyperv.go index d6539d328..e93a885eb 100644 --- a/common/powershell/hyperv/hyperv.go +++ b/common/powershell/hyperv/hyperv.go @@ -32,20 +32,27 @@ type scriptOptions struct { func GetHostAdapterIpAddressForSwitch(switchName string) (string, error) { var script = ` param([string]$switchName, [int]$addressIndex) - $HostVMAdapter = Hyper-V\Get-VMNetworkAdapter -ManagementOS -SwitchName $switchName if ($HostVMAdapter){ - $HostNetAdapter = Get-NetAdapter | ?{ $_.DeviceId -eq $HostVMAdapter.DeviceId } - if ($HostNetAdapter){ - $HostNetAdapterIfIndex = @() - $HostNetAdapterIfIndex += $HostNetAdapter.ifIndex - $HostNetAdapterConfiguration = @(get-wmiobject win32_networkadapterconfiguration -filter "IPEnabled = 'TRUE'") | Where-Object { $HostNetAdapterIfIndex.Contains($_.InterfaceIndex) } - if ($HostNetAdapterConfiguration){ - return @($HostNetAdapterConfiguration.IpAddress)[$addressIndex] - } - } + $HostNetAdapter = Get-NetAdapter | Where-Object { $_.DeviceId -eq $HostVMAdapter.DeviceId } + if ($HostNetAdapter){ + $HostNetAdapterIfIndex = @() + $HostNetAdapterIfIndex += $HostNetAdapter.ifIndex + $HostNetAdapterConfiguration = @(get-wmiobject win32_networkadapterconfiguration -filter "IPEnabled = 'TRUE'") | Where-Object { $HostNetAdapterIfIndex.Contains($_.InterfaceIndex) } + if ($HostNetAdapterConfiguration){ + return @($HostNetAdapterConfiguration.IpAddress)[$addressIndex] + } + } +} +else { + $HostNetAdapterConfiguration=@(Get-NetIPAddress -CimSession $env:computername -AddressFamily IPv4 | Where-Object { ( $_.InterfaceAlias -notmatch 'Loopback' ) -and ( $_.SuffixOrigin -notmatch "Link" )}) + if ($HostNetAdapterConfiguration) { + return @($HostNetAdapterConfiguration.IpAddress)[$addressIndex] + } + else { + return $false + } } -return $false ` var ps powershell.PowerShellCmd