Skip to content

Commit 328e22b

Browse files
committed
Adds PortType management in VMSwitchPortMonitorMode.psm1
1 parent 524c294 commit 328e22b

File tree

1 file changed

+57
-28
lines changed

1 file changed

+57
-28
lines changed

VMSwitchPortMonitorMode.psm1

Lines changed: 57 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,36 @@ Add-Type -TypeDefinition @"
2323
Destination,
2424
Source
2525
}
26+
27+
[System.Flags]
28+
public enum PortType
29+
{
30+
Host = 1,
31+
External = 2
32+
}
2633
}
2734
"@
2835

29-
function GetSwitchEthernetPortAllocationSettingData($vswitchName) {
36+
function GetSwitchEthernetPortAllocationSettingData($vswitchName, $portType) {
3037
$eps = @()
31-
$eps += gwmi -Namespace $ns -Class Msvm_ExternalEthernetPort
32-
$eps += gwmi -Namespace $ns -Class Msvm_InternalEthernetPort
38+
if($portType -band [cloudbase.PortType]::External) {
39+
$eps += gwmi -Namespace $ns -Class Msvm_ExternalEthernetPort
40+
}
41+
if($portType -band [cloudbase.PortType]::Host) {
42+
$eps += gwmi -Namespace $ns -Class Msvm_InternalEthernetPort
43+
}
44+
3345
foreach($ep in $eps) {
3446
$lep1 = gwmi -Namespace $ns -Query "ASSOCIATORS OF {$ep} WHERE ResultClass=Msvm_LANEndpoint AssocClass=Msvm_EthernetDeviceSAPImplementation"
3547
if($lep1) {
3648
$lep2 = gwmi -Namespace $ns -Query "ASSOCIATORS OF {$lep1} WHERE ResultClass=Msvm_LANEndpoint AssocClass=Msvm_ActiveConnection"
3749
$eswp = gwmi -Namespace $ns -Query "ASSOCIATORS OF {$lep2} WHERE ResultClass=Msvm_EthernetSwitchPort AssocClass=Msvm_EthernetDeviceSAPImplementation"
3850
$sw = gwmi -Namespace $ns -Query "ASSOCIATORS OF {$eswp} WHERE ResultClass=Msvm_VirtualEthernetSwitch"
3951
if($sw.ElementName -eq $vswitchName) {
40-
return gwmi -Namespace $ns -Query "ASSOCIATORS OF {$eswp} WHERE ResultClass=Msvm_EthernetPortAllocationSettingData AssocClass=Msvm_ElementSettingData"
52+
gwmi -Namespace $ns -Query "ASSOCIATORS OF {$eswp} WHERE ResultClass=Msvm_EthernetPortAllocationSettingData AssocClass=Msvm_ElementSettingData"
4153
}
4254
}
4355
}
44-
45-
throw "No internal or external VMSwitch named ""$vswitchName"" was found"
4656
}
4757

4858
function CheckJob($out) {
@@ -74,39 +84,58 @@ function Get-VMSwitchPortMonitorMode() {
7484
)
7585

7686
process {
77-
$epasd = GetSwitchEthernetPortAllocationSettingData $SwitchName
78-
$espssd = GetEthernetSwitchPortSecuritySettingData $epasd
79-
if ($espssd) {
80-
[cloudbase.PortMonitorMode]$espssd.MonitorMode
81-
} else {
82-
[cloudbase.PortMonitorMode]::None
87+
$portTypes = @([cloudbase.PortType]::External, [cloudbase.PortType]::Host)
88+
89+
foreach($portType in $portTypes) {
90+
$epasds = GetSwitchEthernetPortAllocationSettingData $SwitchName $portType
91+
foreach($epasd in $epasds) {
92+
$monitorModeInfo = New-Object -TypeName PSObject
93+
Add-Member -InputObject $monitorModeInfo -MemberType NoteProperty -Name "PortType" -Value $portType
94+
95+
$espssd = GetEthernetSwitchPortSecuritySettingData $epasd
96+
if ($espssd) {
97+
$mode = [cloudbase.PortMonitorMode]$espssd.MonitorMode
98+
} else {
99+
$mode = [cloudbase.PortMonitorMode]::None
100+
}
101+
102+
Add-Member -InputObject $monitorModeInfo -MemberType NoteProperty -Name "MonitorMode" -Value $mode
103+
$monitorModeInfo
104+
}
83105
}
84106
}
85107
}
86108

87109
function Set-VMSwitchPortMonitorMode() {
88110
param(
89111
[Parameter(ValueFromPipeline=$true, Position=0, Mandatory=$true)] [string] $SwitchName,
90-
[Parameter(Position=1, Mandatory=$true)] [cloudbase.PortMonitorMode] $MonitorMode
112+
[Parameter(Position=1, Mandatory=$true)] [cloudbase.PortMonitorMode] $MonitorMode,
113+
[Parameter(Position=2)] [cloudbase.PortType] $PortType = [cloudbase.PortType]::External -bor [cloudbase.PortType]::Host
91114
)
92115

93116
process {
94-
$epasd = GetSwitchEthernetPortAllocationSettingData $SwitchName
95-
$espssd = GetEthernetSwitchPortSecuritySettingData $epasd
96-
97-
if ($espssd) {
98-
if($espssd.MonitorMode -ne [int]$MonitorMode) {
99-
$espssd.MonitorMode = [int]$MonitorMode
100-
$svc = gwmi -Namespace $ns -Class Msvm_VirtualEthernetSwitchManagementService
101-
CheckJob $svc.ModifyFeatureSettings(@($espssd.GetText(1)))
102-
}
117+
$epasds = GetSwitchEthernetPortAllocationSettingData $SwitchName $PortType
118+
if(!$epasds) {
119+
throw "Port for VMSwitch named ""$vswitchName"" not found"
103120
} else {
104-
if($MonitorMode -ne [int][cloudbase.PortMonitorMode]::None) {
105-
$espssd = gwmi -Namespace $ns -Class Msvm_EthernetSwitchPortSecuritySettingData | where { $_.InstanceId.EndsWith("\Default") }
106-
$espssd.MonitorMode = [int]$MonitorMode
107-
$svc = gwmi -Namespace $ns -Class Msvm_VirtualEthernetSwitchManagementService
108-
CheckJob $svc.AddFeatureSettings($epasd, @($espssd.GetText(1)))
109-
}
121+
foreach($epasd in $epasds) {
122+
$espssd = GetEthernetSwitchPortSecuritySettingData $epasd
123+
124+
if ($espssd) {
125+
if($espssd.MonitorMode -ne [int]$MonitorMode) {
126+
$espssd.MonitorMode = [int]$MonitorMode
127+
$svc = gwmi -Namespace $ns -Class Msvm_VirtualEthernetSwitchManagementService
128+
CheckJob $svc.ModifyFeatureSettings(@($espssd.GetText(1)))
129+
}
130+
} else {
131+
if($MonitorMode -ne [int][cloudbase.PortMonitorMode]::None) {
132+
$espssd = gwmi -Namespace $ns -Class Msvm_EthernetSwitchPortSecuritySettingData | where { $_.InstanceId.EndsWith("\Default") }
133+
$espssd.MonitorMode = [int]$MonitorMode
134+
$svc = gwmi -Namespace $ns -Class Msvm_VirtualEthernetSwitchManagementService
135+
CheckJob $svc.AddFeatureSettings($epasd, @($espssd.GetText(1)))
136+
}
137+
}
138+
}
110139
}
111140
}
112141
}

0 commit comments

Comments
 (0)