-
Notifications
You must be signed in to change notification settings - Fork 63
/
VMSwitchPortMonitorMode.psm1
executable file
·146 lines (130 loc) · 5.69 KB
/
VMSwitchPortMonitorMode.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Copyright 2014 Cloudbase Solutions Srl
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
$ns = "root\virtualization\v2"
Add-Type -TypeDefinition @"
namespace cloudbase
{
public enum PortMonitorMode
{
None,
Destination,
Source
}
[System.Flags]
public enum PortType
{
Host = 1,
External = 2
}
}
"@
function GetSwitchEthernetPortAllocationSettingData($vswitchName, $portType) {
$eps = @()
if($portType -band [cloudbase.PortType]::External) {
$eps += gwmi -Namespace $ns -Class Msvm_ExternalEthernetPort
}
if($portType -band [cloudbase.PortType]::Host) {
$eps += gwmi -Namespace $ns -Class Msvm_InternalEthernetPort
}
foreach($ep in $eps) {
$lep1 = gwmi -Namespace $ns -Query "ASSOCIATORS OF {$ep} WHERE ResultClass=Msvm_LANEndpoint AssocClass=Msvm_EthernetDeviceSAPImplementation"
if($lep1) {
$lep2 = gwmi -Namespace $ns -Query "ASSOCIATORS OF {$lep1} WHERE ResultClass=Msvm_LANEndpoint AssocClass=Msvm_ActiveConnection"
$eswp = gwmi -Namespace $ns -Query "ASSOCIATORS OF {$lep2} WHERE ResultClass=Msvm_EthernetSwitchPort AssocClass=Msvm_EthernetDeviceSAPImplementation"
$sw = gwmi -Namespace $ns -Query "ASSOCIATORS OF {$eswp} WHERE ResultClass=Msvm_VirtualEthernetSwitch"
if($sw.ElementName -eq $vswitchName) {
gwmi -Namespace $ns -Query "ASSOCIATORS OF {$eswp} WHERE ResultClass=Msvm_EthernetPortAllocationSettingData AssocClass=Msvm_ElementSettingData"
}
}
}
}
function CheckJob($out) {
if($out.ReturnValue -ne 0) {
if($out.ReturnValue -ne 4096) {
throw "Job failed with status: ${$out.ReturnValue}"
} else {
do {
$job = [wmi]$out.Job
if ($job.JobState -ne 4096) {
if ($job.JobState -eq 7) {
return
} else {
throw $job.ErrorDescription
}
} else {
Start-Sleep -Milliseconds 200
}
} while ($true)
}
}
}
function GetEthernetSwitchPortSecuritySettingData($epasd) {
return gwmi -Namespace $ns -Query "ASSOCIATORS OF {$epasd} WHERE ResultClass=Msvm_EthernetSwitchPortSecuritySettingData AssocClass=Msvm_EthernetPortSettingDataComponent"
}
function Get-VMSwitchPortMonitorMode() {
param(
[Parameter(ValueFromPipeline=$true, Position=0, Mandatory=$true)] [string] $SwitchName
)
process {
$portTypes = @([cloudbase.PortType]::External, [cloudbase.PortType]::Host)
foreach($portType in $portTypes) {
$epasds = GetSwitchEthernetPortAllocationSettingData $SwitchName $portType
foreach($epasd in $epasds) {
$monitorModeInfo = New-Object -TypeName PSObject
Add-Member -InputObject $monitorModeInfo -MemberType NoteProperty -Name "PortType" -Value $portType
$espssd = GetEthernetSwitchPortSecuritySettingData $epasd
if ($espssd) {
$mode = [cloudbase.PortMonitorMode]$espssd.MonitorMode
} else {
$mode = [cloudbase.PortMonitorMode]::None
}
Add-Member -InputObject $monitorModeInfo -MemberType NoteProperty -Name "MonitorMode" -Value $mode
$monitorModeInfo
}
}
}
}
function Set-VMSwitchPortMonitorMode() {
param(
[Parameter(ValueFromPipeline=$true, Position=0, Mandatory=$true)] [string] $SwitchName,
[Parameter(Position=1, Mandatory=$true)] [cloudbase.PortMonitorMode] $MonitorMode,
[Parameter(Position=2)] [cloudbase.PortType] $PortType = [cloudbase.PortType]::External -bor [cloudbase.PortType]::Host
)
process {
$epasds = GetSwitchEthernetPortAllocationSettingData $SwitchName $PortType
if(!$epasds) {
throw "Port for VMSwitch named ""$SwitchName"" not found"
} else {
foreach($epasd in $epasds) {
$espssd = GetEthernetSwitchPortSecuritySettingData $epasd
if ($espssd) {
if($espssd.MonitorMode -ne [int]$MonitorMode) {
$espssd.MonitorMode = [int]$MonitorMode
$svc = gwmi -Namespace $ns -Class Msvm_VirtualEthernetSwitchManagementService
CheckJob $svc.ModifyFeatureSettings(@($espssd.GetText(1)))
}
} else {
if($MonitorMode -ne [int][cloudbase.PortMonitorMode]::None) {
$espssd = gwmi -Namespace $ns -Class Msvm_EthernetSwitchPortSecuritySettingData | where { $_.InstanceId.EndsWith("\Default") }
$espssd.MonitorMode = [int]$MonitorMode
$svc = gwmi -Namespace $ns -Class Msvm_VirtualEthernetSwitchManagementService
CheckJob $svc.AddFeatureSettings($epasd, @($espssd.GetText(1)))
}
}
}
}
}
}
Export-ModuleMember Get-VMSwitchPortMonitorMode
Export-ModuleMember Set-VMSwitchPortMonitorMode