Merge pull request #8017 from marcinbojko/master

hyper-v fix when management interface is not part of virtual switch
This commit is contained in:
Megan Marsh 2019-09-03 11:15:51 -07:00 committed by GitHub
commit 9f041216ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 11 deletions

View File

@ -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