Reply to Message

View discussion in a popup

Replying to:
Halukkocaman
Enthusiast
Enthusiast

Remove Port Mirroring

Hi All,

I would like to automate adding VM to port mirroring session and remove it after given time. I found the script LucD posted for "Add VM to a Port Mirroring Session" but I cannot figure out how can I remove them. Anyone has any suggestion?

$dvSwName = 'dvSw1'

$mirrorSessionName = 'Test'

$vmName = 'VM2'

$Ingress = $false

$Egress = $true

$dvSw = Get-VDSwitch -Name $dvSwName

$vm = Get-VM -Name $vmName

$vmNic = $vm.ExtensionData.Config.Hardware.Device |

    where{$_.Backing -is [VMware.Vim.VirtualEthernetCardDistributedVirtualPortBackingInfo]}

$spec = New-Object VMware.Vim.VMwareDVSConfigSpec

foreach($mirrorSession in $dvSw.ExtensionData.Config.VspanSession){

    if($mirrorSession.Name -eq $mirrorSessionName){

        $vspan = New-Object VMware.Vim.VMwareDVSVspanConfigSpec

        $vspan.Operation = [VMware.Vim.ConfigSpecOperation]::edit

        $vmInRc = $mirrorSession.SourcePortReceived | where{$_.PortKey -contains $vmNic.Backing.Port.PortKey}

        if($Ingress -and !$vmInRc){

            $mirrorSession.SourcePortReceived.PortKey += $vmNic.Backing.Port.PortKey

        }

        $vmInTx = $mirrorSession.SourcePortTRansmitted | where{$_.PortKey -contains $vmNic.Backing.Port.PortKey}

        if($Egress -and !$vmInTx){

            $mirrorSession.SourcePortTransmitted.PortKey += $vmNic.Backing.Port.PortKey

        }

        $vspan.vspanSession = $mirrorSession

        $spec.vspanConfigSpec += $vspan

    }

}

$spec.ConfigVersion = $dvSw.ExtensionData.Config.ConfigVersion

$dvSw.ExtensionData.ReconfigureDvs($spec)

Reply
0 Kudos