The reward for effort when scripting is almost always worth it. Today I had the task of removing a particular AntiVirus product from a 36 host VDI cluster. Part of this requires the removal of NSX-Manager and all the groovy components it uses to do it’s thing, including a vSwitch on every host, with multiple port-groups.
I instantly knew this would take significant time if I went down the click-click-click track for all 36 hosts. Here’s how I went about it using PowerCLI.
First step is to identify the name of the vwsitch in question
Get-vSwitch shows all the vSwitches in the connected VI server’s inventory. Here it is, “vmservice-vswitch”
Let’s get a list of all the hosts with this vswitch
1 |
Get-VirtualSwitch -Name "vmservice-vswitch | FT VMHost |
We need to kind of work backwards here;
- 1- Remove the VMkernel ports
- 2- Remove the Virtual Port Groups
- 3- Remove the Virtual switches.
The removal order is required as you can’t delete these objects while they contain other objects,
Let’s get a list of the applicable vmkernel ports;
1 |
Get-VirtualSwitch -Name "vmservice-vswitch" | Get-VirtualPortGroup | Get-VMHostNetworkAdaptor |
Looks consistent.
Next, remove the Virtual Port Group.
1 |
Get-VirtualSwitch -Name "vmservice-vswitch" | Get-VirtualPortGroup | Remove-VirtualPortGroup -Confirm:$false |
Nervous ? use the -Whatif switch to do a sanity check. Looks right.
Check they’re gone;
1 |
Get-VirtualSwitch -Name "vmservice-vswitch" | Get-VirtualPortGroup |
Where now good to go. Let’s remove all the vSwitches.
1 |
Get-VirtualSwitch -Name "vmservice-vswitch" | Remove-VirtualSwitch -Confirm:$false |
Gone ? You bet.
Checking back in the UI, there’s a raft of completed tasks.
Of course you can check using Get-Tasks also.
Do yourself a favour and have things like this in your “cookbook” so you can save yourself time not having to do repetitive, mundane tasks.
Over and out.