Tuesday, February 16, 2016

script/tool to remove vmkernel/vm portgroups on all vmware hosts

We all know(assuming that you are familiar with vmware) how to remove portgroups on esxi hosts but what if you have to do that on a lot of hosts? and if you are like me who likes quick, dirty and lazy ways of achieving things then here is something which might suit your taste buds.

first of all let us choose a cluster on which we want to perform this activity.

$cluster="deadpool"

$pg="galactus"

get-cluster$cluster|get-vmhost|selectname|sort

Above i have chosen the cluster deadpool (the new standalone movie version not the crappy x men origins wolverine version) and the portgroup is galactus. The above gives me

Name

----

Wolverine1.dc.universe

wolverine2.dc.universe

wolverine3.dc.universe

let us find out on which host we have the portgroup that we want to remove.

$cluster = "deadpool"

$pg=
"galactus"

foreach ($vmhost in (get-cluster $cluster | get-vmhost |
sort))

{

get-vmhost $vmhost |
get-virtualportgroup -name $pg

}

NameKeyVLanId PortBinding NumPorts

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

galactuskey-vim.host.PortGroup-gala... 0

galactuskey-vim.host.PortGroup-gala... 0

get-virtualportgroup : 2/17/2016 11:07:43 AMGet-VirtualPortGroupVirtualPortGroup with name 'galactus'
was not found using the specified

so it means only one host now has this portgroup and on the other one the script successfully remove the portgroup. Let us see why script was able to remove the portgroup on one host and couldnt on another one.

This is why. This galactus portgroup on this host is not a regular vm portgroup but a vmkernel portgroup. First we have to remove the vmkernel and then we have to remove the portgroup.

$vmk=Get-VMHostNetworkAdapter-VMHost$vmhost|wherePortgroupName-eq$pg

Remove-VMHostNetworkAdapter-Nic$vmk-confirm:$false

The above should remove the vmkernel. The lines are self explanatory. The first line fetched the vmkernel. 2nd line removes the vmkernel but we want to do this on only that portgroup in which there is a vmkernel present.

$vmk=Get-VMHostNetworkAdapter-VMHost$vmhost|wherePortgroupName-eq$pg

if($vmk-ne$null){

Remove-VMHostNetworkAdapter-Nic$vmk-confirm:$false

}

basically the above will check whether the number of vmkernel is $null (meaning 0 or absent) then it will proceed further to remove the vmkernel, otherwise it will skip to the next scriptblock.

About Me

A passionate IT Professional with an endless fetish for innovative Science & Technology.I am a lot of things and a few among them areAn avid gamer,loves photography(not anymore),love a lot of variety of music, movies (foreign),like a lot of manga, anime series and most importantly love life.