|
1 |
| -# Copyright 2014 Cloudbase Solutions Srl |
2 |
| -# |
3 |
| -# Licensed under the Apache License, Version 2.0 (the "License"); you may |
4 |
| -# not use this file except in compliance with the License. You may obtain |
5 |
| -# a copy of the License at |
6 |
| -# |
7 |
| -# http://www.apache.org/licenses/LICENSE-2.0 |
8 |
| -# |
9 |
| -# Unless required by applicable law or agreed to in writing, software |
10 |
| -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
11 |
| -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
12 |
| -# License for the specific language governing permissions and limitations |
13 |
| -# under the License. |
14 |
| - |
15 |
| -$ns = "root\virtualization\v2" |
16 |
| - |
17 |
| -Add-Type -TypeDefinition @" |
18 |
| - namespace cloudbase |
19 |
| - { |
20 |
| - public enum PortMonitorMode |
21 |
| - { |
22 |
| - None, |
23 |
| - Destination, |
24 |
| - Source |
25 |
| - } |
26 |
| - } |
27 |
| -"@ |
28 |
| - |
29 |
| -function GetSwitchEthernetPortAllocationSettingData($vswitchName) |
30 |
| -{ |
31 |
| - $eps = @() |
32 |
| - $eps += gwmi -Namespace $ns -Class Msvm_ExternalEthernetPort |
33 |
| - $eps += gwmi -Namespace $ns -Class Msvm_InternalEthernetPort |
34 |
| - foreach($ep in $eps) |
35 |
| - { |
36 |
| - $lep1 = gwmi -Namespace $ns -Query "ASSOCIATORS OF {$ep} WHERE ResultClass=Msvm_LANEndpoint AssocClass=Msvm_EthernetDeviceSAPImplementation" |
37 |
| - if($lep1) { |
38 |
| - $lep2 = gwmi -Namespace $ns -Query "ASSOCIATORS OF {$lep1} WHERE ResultClass=Msvm_LANEndpoint AssocClass=Msvm_ActiveConnection" |
39 |
| - $eswp = gwmi -Namespace $ns -Query "ASSOCIATORS OF {$lep2} WHERE ResultClass=Msvm_EthernetSwitchPort AssocClass=Msvm_EthernetDeviceSAPImplementation" |
40 |
| - $sw = gwmi -Namespace $ns -Query "ASSOCIATORS OF {$eswp} WHERE ResultClass=Msvm_VirtualEthernetSwitch" |
41 |
| - if($sw.ElementName -eq $vswitchName) |
42 |
| - { |
43 |
| - return gwmi -Namespace $ns -Query "ASSOCIATORS OF {$eswp} WHERE ResultClass=Msvm_EthernetPortAllocationSettingData AssocClass=Msvm_ElementSettingData" |
44 |
| - } |
45 |
| - } |
46 |
| - } |
47 |
| - |
48 |
| - throw "No internal or external VMSwitch named ""$vswitchName"" was found" |
49 |
| -} |
50 |
| - |
51 |
| -function CheckJob($out) { |
52 |
| - if($out.ReturnValue -ne 0) { |
53 |
| - if($out.ReturnValue -ne 4096) { |
54 |
| - throw "Job failed with status: ${$out.ReturnValue}" |
55 |
| - } else { |
56 |
| - do { |
57 |
| - $job = [wmi]$out.Job |
58 |
| - if ($job.JobState -ne 4096) { |
59 |
| - if ($job.JobState -eq 7) { |
60 |
| - return |
61 |
| - } else { |
62 |
| - throw $job.ErrorDescription |
63 |
| - } |
64 |
| - } |
65 |
| - } while ($true) |
66 |
| - } |
67 |
| - } |
68 |
| -} |
69 |
| - |
70 |
| -function GetEthernetSwitchPortSecuritySettingData($epasd) { |
71 |
| - return gwmi -Namespace $ns -Query "ASSOCIATORS OF {$epasd} WHERE ResultClass=Msvm_EthernetSwitchPortSecuritySettingData AssocClass=Msvm_EthernetPortSettingDataComponent" |
72 |
| -} |
73 |
| - |
74 |
| -function Get-VMSwitchPortMonitorMode() { |
75 |
| - param( |
76 |
| - [Parameter(ValueFromPipeline=$true, Position=0, Mandatory=$true)] [string] $SwitchName |
77 |
| - ) |
78 |
| - |
79 |
| - process { |
80 |
| - $epasd = GetSwitchEthernetPortAllocationSettingData $SwitchName |
81 |
| - $espssd = GetEthernetSwitchPortSecuritySettingData $epasd |
82 |
| - if ($espssd) { |
83 |
| - [cloudbase.PortMonitorMode]$espssd.MonitorMode |
84 |
| - } else { |
85 |
| - [cloudbase.PortMonitorMode]::None |
86 |
| - } |
87 |
| - } |
88 |
| -} |
89 |
| - |
90 |
| -function Set-VMSwitchPortMonitorMode() { |
91 |
| - param( |
92 |
| - [Parameter(ValueFromPipeline=$true, Position=0, Mandatory=$true)] [string] $SwitchName, |
93 |
| - [Parameter(Position=1, Mandatory=$true)] [cloudbase.PortMonitorMode] $MonitorMode |
94 |
| - ) |
95 |
| - |
96 |
| - process { |
97 |
| - $epasd = GetSwitchEthernetPortAllocationSettingData $SwitchName |
98 |
| - $espssd = GetEthernetSwitchPortSecuritySettingData $epasd |
99 |
| - |
100 |
| - if ($espssd) { |
101 |
| - if($espssd.MonitorMode -ne [int]$MonitorMode) { |
102 |
| - $espssd.MonitorMode = [int]$MonitorMode |
103 |
| - $svc = gwmi -Namespace $ns -Class Msvm_VirtualEthernetSwitchManagementService |
104 |
| - CheckJob $svc.ModifyFeatureSettings(@($espssd.GetText(1))) |
105 |
| - } |
106 |
| - } else { |
107 |
| - if($MonitorMode -ne [int][cloudbase.PortMonitorMode]::None) { |
108 |
| - $espssd = gwmi -Namespace $ns -Class Msvm_EthernetSwitchPortSecuritySettingData | where { $_.InstanceId.EndsWith("\Default") } |
109 |
| - $espssd.MonitorMode = [int]$MonitorMode |
110 |
| - $svc = gwmi -Namespace $ns -Class Msvm_VirtualEthernetSwitchManagementService |
111 |
| - CheckJob $svc.AddFeatureSettings($epasd, @($espssd.GetText(1))) |
112 |
| - } |
113 |
| - } |
114 |
| - } |
115 |
| -} |
116 |
| - |
117 |
| -Export-ModuleMember Get-VMSwitchPortMonitorMode |
118 |
| -Export-ModuleMember Set-VMSwitchPortMonitorMode |
| 1 | +# Copyright 2014 Cloudbase Solutions Srl |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +# not use this file except in compliance with the License. You may obtain |
| 5 | +# a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | + |
| 15 | +$ns = "root\virtualization\v2" |
| 16 | + |
| 17 | +Add-Type -TypeDefinition @" |
| 18 | + namespace cloudbase |
| 19 | + { |
| 20 | + public enum PortMonitorMode |
| 21 | + { |
| 22 | + None, |
| 23 | + Destination, |
| 24 | + Source |
| 25 | + } |
| 26 | + } |
| 27 | +"@ |
| 28 | + |
| 29 | +function GetSwitchEthernetPortAllocationSettingData($vswitchName) |
| 30 | +{ |
| 31 | + $eps = @() |
| 32 | + $eps += gwmi -Namespace $ns -Class Msvm_ExternalEthernetPort |
| 33 | + $eps += gwmi -Namespace $ns -Class Msvm_InternalEthernetPort |
| 34 | + foreach($ep in $eps) |
| 35 | + { |
| 36 | + $lep1 = gwmi -Namespace $ns -Query "ASSOCIATORS OF {$ep} WHERE ResultClass=Msvm_LANEndpoint AssocClass=Msvm_EthernetDeviceSAPImplementation" |
| 37 | + if($lep1) { |
| 38 | + $lep2 = gwmi -Namespace $ns -Query "ASSOCIATORS OF {$lep1} WHERE ResultClass=Msvm_LANEndpoint AssocClass=Msvm_ActiveConnection" |
| 39 | + $eswp = gwmi -Namespace $ns -Query "ASSOCIATORS OF {$lep2} WHERE ResultClass=Msvm_EthernetSwitchPort AssocClass=Msvm_EthernetDeviceSAPImplementation" |
| 40 | + $sw = gwmi -Namespace $ns -Query "ASSOCIATORS OF {$eswp} WHERE ResultClass=Msvm_VirtualEthernetSwitch" |
| 41 | + if($sw.ElementName -eq $vswitchName) |
| 42 | + { |
| 43 | + return gwmi -Namespace $ns -Query "ASSOCIATORS OF {$eswp} WHERE ResultClass=Msvm_EthernetPortAllocationSettingData AssocClass=Msvm_ElementSettingData" |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + throw "No internal or external VMSwitch named ""$vswitchName"" was found" |
| 49 | +} |
| 50 | + |
| 51 | +function CheckJob($out) { |
| 52 | + if($out.ReturnValue -ne 0) { |
| 53 | + if($out.ReturnValue -ne 4096) { |
| 54 | + throw "Job failed with status: ${$out.ReturnValue}" |
| 55 | + } else { |
| 56 | + do { |
| 57 | + $job = [wmi]$out.Job |
| 58 | + if ($job.JobState -ne 4096) { |
| 59 | + if ($job.JobState -eq 7) { |
| 60 | + return |
| 61 | + } else { |
| 62 | + throw $job.ErrorDescription |
| 63 | + } |
| 64 | + } |
| 65 | + } while ($true) |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +function GetEthernetSwitchPortSecuritySettingData($epasd) { |
| 71 | + return gwmi -Namespace $ns -Query "ASSOCIATORS OF {$epasd} WHERE ResultClass=Msvm_EthernetSwitchPortSecuritySettingData AssocClass=Msvm_EthernetPortSettingDataComponent" |
| 72 | +} |
| 73 | + |
| 74 | +function Get-VMSwitchPortMonitorMode() { |
| 75 | + param( |
| 76 | + [Parameter(ValueFromPipeline=$true, Position=0, Mandatory=$true)] [string] $SwitchName |
| 77 | + ) |
| 78 | + |
| 79 | + process { |
| 80 | + $epasd = GetSwitchEthernetPortAllocationSettingData $SwitchName |
| 81 | + $espssd = GetEthernetSwitchPortSecuritySettingData $epasd |
| 82 | + if ($espssd) { |
| 83 | + [cloudbase.PortMonitorMode]$espssd.MonitorMode |
| 84 | + } else { |
| 85 | + [cloudbase.PortMonitorMode]::None |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +function Set-VMSwitchPortMonitorMode() { |
| 91 | + param( |
| 92 | + [Parameter(ValueFromPipeline=$true, Position=0, Mandatory=$true)] [string] $SwitchName, |
| 93 | + [Parameter(Position=1, Mandatory=$true)] [cloudbase.PortMonitorMode] $MonitorMode |
| 94 | + ) |
| 95 | + |
| 96 | + process { |
| 97 | + $epasd = GetSwitchEthernetPortAllocationSettingData $SwitchName |
| 98 | + $espssd = GetEthernetSwitchPortSecuritySettingData $epasd |
| 99 | + |
| 100 | + if ($espssd) { |
| 101 | + if($espssd.MonitorMode -ne [int]$MonitorMode) { |
| 102 | + $espssd.MonitorMode = [int]$MonitorMode |
| 103 | + $svc = gwmi -Namespace $ns -Class Msvm_VirtualEthernetSwitchManagementService |
| 104 | + CheckJob $svc.ModifyFeatureSettings(@($espssd.GetText(1))) |
| 105 | + } |
| 106 | + } else { |
| 107 | + if($MonitorMode -ne [int][cloudbase.PortMonitorMode]::None) { |
| 108 | + $espssd = gwmi -Namespace $ns -Class Msvm_EthernetSwitchPortSecuritySettingData | where { $_.InstanceId.EndsWith("\Default") } |
| 109 | + $espssd.MonitorMode = [int]$MonitorMode |
| 110 | + $svc = gwmi -Namespace $ns -Class Msvm_VirtualEthernetSwitchManagementService |
| 111 | + CheckJob $svc.AddFeatureSettings($epasd, @($espssd.GetText(1))) |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +Export-ModuleMember Get-VMSwitchPortMonitorMode |
| 118 | +Export-ModuleMember Set-VMSwitchPortMonitorMode |
0 commit comments