Skip to content

Commit

Permalink
Adds PortType management in VMSwitchPortMonitorMode.psm1
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpilotti committed Sep 26, 2014
1 parent 524c294 commit 328e22b
Showing 1 changed file with 57 additions and 28 deletions.
85 changes: 57 additions & 28 deletions VMSwitchPortMonitorMode.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,36 @@ Add-Type -TypeDefinition @"
Destination,
Source
}
[System.Flags]
public enum PortType
{
Host = 1,
External = 2
}
}
"@

function GetSwitchEthernetPortAllocationSettingData($vswitchName) {
function GetSwitchEthernetPortAllocationSettingData($vswitchName, $portType) {
$eps = @()
$eps += gwmi -Namespace $ns -Class Msvm_ExternalEthernetPort
$eps += gwmi -Namespace $ns -Class Msvm_InternalEthernetPort
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) {
return gwmi -Namespace $ns -Query "ASSOCIATORS OF {$eswp} WHERE ResultClass=Msvm_EthernetPortAllocationSettingData AssocClass=Msvm_ElementSettingData"
gwmi -Namespace $ns -Query "ASSOCIATORS OF {$eswp} WHERE ResultClass=Msvm_EthernetPortAllocationSettingData AssocClass=Msvm_ElementSettingData"
}
}
}

throw "No internal or external VMSwitch named ""$vswitchName"" was found"
}

function CheckJob($out) {
Expand Down Expand Up @@ -74,39 +84,58 @@ function Get-VMSwitchPortMonitorMode() {
)

process {
$epasd = GetSwitchEthernetPortAllocationSettingData $SwitchName
$espssd = GetEthernetSwitchPortSecuritySettingData $epasd
if ($espssd) {
[cloudbase.PortMonitorMode]$espssd.MonitorMode
} else {
[cloudbase.PortMonitorMode]::None
$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=1, Mandatory=$true)] [cloudbase.PortMonitorMode] $MonitorMode,
[Parameter(Position=2)] [cloudbase.PortType] $PortType = [cloudbase.PortType]::External -bor [cloudbase.PortType]::Host
)

process {
$epasd = GetSwitchEthernetPortAllocationSettingData $SwitchName
$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)))
}
$epasds = GetSwitchEthernetPortAllocationSettingData $SwitchName $PortType
if(!$epasds) {
throw "Port for VMSwitch named ""$vswitchName"" not found"
} 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)))
}
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)))
}
}
}
}
}
}
Expand Down

0 comments on commit 328e22b

Please sign in to comment.