@@ -23,26 +23,36 @@ Add-Type -TypeDefinition @"
23
23
Destination,
24
24
Source
25
25
}
26
+
27
+ [System.Flags]
28
+ public enum PortType
29
+ {
30
+ Host = 1,
31
+ External = 2
32
+ }
26
33
}
27
34
"@
28
35
29
- function GetSwitchEthernetPortAllocationSettingData ($vswitchName ) {
36
+ function GetSwitchEthernetPortAllocationSettingData ($vswitchName , $portType ) {
30
37
$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
+
33
45
foreach ($ep in $eps ) {
34
46
$lep1 = gwmi - Namespace $ns - Query " ASSOCIATORS OF {$ep } WHERE ResultClass=Msvm_LANEndpoint AssocClass=Msvm_EthernetDeviceSAPImplementation"
35
47
if ($lep1 ) {
36
48
$lep2 = gwmi - Namespace $ns - Query " ASSOCIATORS OF {$lep1 } WHERE ResultClass=Msvm_LANEndpoint AssocClass=Msvm_ActiveConnection"
37
49
$eswp = gwmi - Namespace $ns - Query " ASSOCIATORS OF {$lep2 } WHERE ResultClass=Msvm_EthernetSwitchPort AssocClass=Msvm_EthernetDeviceSAPImplementation"
38
50
$sw = gwmi - Namespace $ns - Query " ASSOCIATORS OF {$eswp } WHERE ResultClass=Msvm_VirtualEthernetSwitch"
39
51
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"
41
53
}
42
54
}
43
55
}
44
-
45
- throw " No internal or external VMSwitch named "" $vswitchName "" was found"
46
56
}
47
57
48
58
function CheckJob ($out ) {
@@ -74,39 +84,58 @@ function Get-VMSwitchPortMonitorMode() {
74
84
)
75
85
76
86
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
+ }
83
105
}
84
106
}
85
107
}
86
108
87
109
function Set-VMSwitchPortMonitorMode () {
88
110
param (
89
111
[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
91
114
)
92
115
93
116
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"
103
120
} 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
+ }
110
139
}
111
140
}
112
141
}
0 commit comments