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)
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)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
This is great if you would like to remove the whole port mirroring session. I was looking to remove a vm out of session.
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)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you. This is what exactly what I was looking for.
Hello @LucD
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.)
You have to give some more details than "... did not work for me"
Any error messages?
Anything in the vpxd log?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
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]}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
You could do
$vmNicInfo = $vm.ExtensionData.Config.Hardware.Device |
where{$_.Backing -is [VMware.Vim.VirtualEthernetCardDistributedVirtualPortBackingInfo] -and $_.DeviceInfo.Label -eq 'Network adapter 1'}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
It works perfectly!
Now I see where I was making a mistake.
I appreciated your time. Thank you again 🙏