PowerCLI

 View Only
Expand all | Collapse all

Remove Port Mirroring

  • 1.  Remove Port Mirroring

    Posted Feb 06, 2017 06:52 PM

    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)



  • 2.  RE: Remove Port Mirroring

    Posted Feb 06, 2017 07:32 PM

    Try like this

    $dvSwName = 'dvSw1' 

    $mirrorSessionName = 'Test' 

    $vmName = 'VM2' 

     

    $dvSw = Get-VDSwitch -Name $dvSwName 

    $vm = Get-VM -Name $vmName 

    $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]::remove

            $vspan.VspanSession += $mirrorSession

            $spec.vspanConfigSpec += $vspan 

        }

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

    $dvSw.ExtensionData.ReconfigureDvs($spec)



  • 3.  RE: Remove Port Mirroring

    Posted Feb 06, 2017 08:02 PM

    This is great if you would like to remove the whole port mirroring session. I was looking to remove a vm out of session.



  • 4.  RE: Remove Port Mirroring
    Best Answer

    Posted Feb 06, 2017 08:47 PM

    Got it, try like this

    $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 = $mirrorSession.SourcePortReceived.PortKey |

                    where{$_ -ne $vmNic.Backing.Port.PortKey}

            } 

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

            if($Egress -and $vmInTx){ 

                $mirrorSession.SourcePortTransmitted.PortKey = $mirrorSession.SourcePortTransmitted.PortKey |

                    where{$_ -ne $vmNic.Backing.Port.PortKey}

            } 

            $vspan.VspanSession += $mirrorSession

            $spec.vspanConfigSpec += $vspan

        }

    }

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

    $dvSw.ExtensionData.ReconfigureDvs($spec)



  • 5.  RE: Remove Port Mirroring

    Posted Feb 06, 2017 08:55 PM

    Thank you. This is what exactly what I was looking for.



  • 6.  RE: Remove Port Mirroring

    Posted May 11, 2021 11:02 PM

    Hello  

    First of all, thanks for sharing such a useful function. I was able to add the VM at the session without any problem, however, this function for removing the VM from the Session did not work for me. I have tried some modifications on the code but no success.

    A workaround for me would be: Remove the whole Session ( I was able to do it ) and then recreate it with the new VMs... that would work for me, however, through PowerCLI seems there's not a possibility to recreate it, ( at least I could not find a way to do it) so I have to insist in removing the VM only and add a new desired VM. ( probably better than messing up with session re-creation.)




  • 7.  RE: Remove Port Mirroring

    Posted May 12, 2021 06:51 AM

    You have to give some more details than "... did not work for me"

    Any error messages?
    Anything in the vpxd log?



  • 8.  RE: Remove Port Mirroring

    Posted May 25, 2021 01:39 PM

    Hello LucD, I apologize for the late answer, I have investigated and tried to modify the code you provided, but I had no success. There's no error either, just info saying the configuration was completed, but nothing has changed on the vspanSession.

    In Taks Events GUI:---------------------------------------------------------
    Task: Reconfigure vSphere Distributed Switch
    05/25/2021, 3:22:42 PM The vSphere Distributed Switch FE-LAB-VDS01 in LAB was reconfigured. Modified: config.configVersion: "244" -> "245"; Added: Deleted: 

    In vpxd log:-------------------------------------------------------------------

     BEGIN task-340194 -- dvs-1426 -- vim.DistributedVirtualSwitch.reconfigure -- 525030df-85d6-6884-ecde-731f47070f12(52ef6c61-8e71-1f06-4c06-337154a72fbd)
    2021-05-25T13:22:42.806Z info vpxd[07339] [Originator@6876 sub=vspan opID=53f92c34] Updating vspanSession config in VDS [FE-LAB-VDS01] from configSpec
    2021-05-25T13:22:42.811Z info vpxd[07339] [Originator@6876 sub=ipfix opID=53f92c34] [Ipfix::ApplySpecToConfigInfo] updating ipfixConfig in VDS [FE-LAB-VDS01] configInfo based on what is set in the configSpec.
    2021-05-25T13:22:42.858Z info vpxd[07612] [Originator@6876 sub=MoCluster opID=HB-host-7344@32208-243b5ee3] Host [vim.HostSystem:host-7344,vsan-lab-esx01.ubisoft.org] has 1 HDCS
    resources
    2021-05-25T13:22:42.873Z info vpxd[07333] [Originator@6876 sub=vpxLro opID=klkrlhvw-dcdn-h5:70218441-ea] [VpxLRO] -- FINISH lro-54326244
    2021-05-25T13:22:42.926Z info vpxd[07339] [Originator@6876 sub=vpxLro opID=53f92c34] [VpxLRO] -- FINISH task-340194

    ---------------------------------


    To give a bit of context if it helps, the VM has 2 NetowrkDevices, but only one of them was configured in the PortMirroring session, which is the one I would like to remove whit this script.  

    Thanks a lot for your time.



  • 9.  RE: Remove Port Mirroring

    Posted May 25, 2021 01:56 PM

    The script will definitely not work when there is more than 1 vNIC.
    You could try to extend the following line to only select the vNIC that was connected to the session.
    That could for example be based on the vNIC name.

    $vmNic = $vm.ExtensionData.Config.Hardware.Device | where{$_.Backing -is [VMware.Vim.VirtualEthernetCardDistributedVirtualPortBackingInfo]} 

     



  • 10.  RE: Remove Port Mirroring

    Posted May 25, 2021 03:14 PM

    Thanks a lot LucD.  Do you mean something like this? 

     

    $vmNicInfo = $vm.ExtensionData.Config.Hardware.Device | where{$_.Backing -is [VMware.Vim.VirtualEthernetCardDistributedVirtualPortBackingInfo]} 
    $vmNic = $vmNicInfo.DeviceInfo | where "label" -eq "Network adapter 1"

     

    I have also tried this:

    $vmNicInfo = $vm.ExtensionData.Config.Hardware.Device | where{$_.Backing -is [VMware.Vim.VirtualEthernetCardDistributedVirtualPortBackingInfo]} 
    $vmNic = $vmNicInfo.Backing.Port.PortKey | Sort-Object | Select-Object -First 1

    I'm not sure if I'm selecting the object " Network adapter 1" in a proper way. Did not work for me so far.


     



  • 11.  RE: Remove Port Mirroring

    Posted May 25, 2021 03:24 PM

    You could do

    $vmNicInfo = $vm.ExtensionData.Config.Hardware.Device | 
        where{$_.Backing -is [VMware.Vim.VirtualEthernetCardDistributedVirtualPortBackingInfo] -and $_.DeviceInfo.Label -eq 'Network adapter 1'} 


  • 12.  RE: Remove Port Mirroring

    Posted May 25, 2021 03:32 PM

    It works perfectly! 

    Now I see where I was making a mistake.

    I appreciated your time. Thank you again