profile for Gajendra D Ambi on Stack Exchange, a network of free, community-driven Q&A sites

Tuesday, December 22, 2015

Shooting the portgroups with powercli

If you are someone who are involved in building VMware infrastructures like me then i am pretty sure you might have had moments of regret creating some portgroups that you didnt wanted or wanted them but not quite the way you liked it. Well, you are not alone. I too have to shoot some portgroups every now and then (almost atleast thrice a week) and here is my way of getting that s**t done...wink wink..
So first we wanna get the variables for this purpose.


#variables
$cluster   = "*"
$portgroup = "VMkernel"

(Now i am not too systematic with commas, indentation and stuff like that, i just go for the kill without having to bother too much about rules of scripting or coding. so dont hate me for it. it works so thats enough for me.)

If you wanna perform this on all clusters then keep that * there or replace that * in the $cluster with the name of your cluster within quotes.

next up is we wanna connect to the vcenter or host or hosts and list all the hosts.


#Connect to vcenter server or a single host or hosts
Connect-VIServer
$vmhosts   = get-cluster "*" | Get-VMHost | sort

So here $vmhosts is the list of all the hosts of the vcenter
Now what if we have these diseased (or soon to be diseased..lol) portgroups are not on all hosts but on just few hosts? so let us filter the the lists of hosts with the hosts with the portgroup name $portgroup in them.

foreach ($vmhost in $vmhosts)
{
$pgs = get-vmhost $vmhost | get-virtualportgroup | select name
}

 So now here $pgs is the list of all the portgroups in the $vmhost. Now let us search for the $portgroup that we wanna remove.



foreach ($vmhost in $vmhosts)
{
$pgs       = get-vmhost $vmhost | get-virtualportgroup | select name
if ($pgs -match "VMkernel")
 {
  $vmk = Get-VMHostNetworkAdapter -VMHost $vmhost | where PortgroupName -eq $portgroup
  Write-Host found $portgroup with vmkernel $vmk on $vmhost -ForegroundColor yellow

 }
}

 So now we can see that we have an if condition where if $pgs (which is a list of all the protgroups in the $vmhost) contains a portgroup by the name $portgroup (in our case VMkernel) then find the vmkernel adapter associated with that portgroup, if not don't bother and move to the next host. Once it finds the host with the portgroup and the vmkernel adapter in it then it will display a message about it's findings. Now that we have found the $vmhost with the portgroup $portgroup with the $vmk adapter in it, we want to remove that.


foreach ($vmhost in $vmhosts)
{
$pgs       = get-vmhost $vmhost | get-virtualportgroup | select name
if ($pgs -match "VMkernel")
 {
  $vmk = Get-VMHostNetworkAdapter -VMHost $vmhost | where PortgroupName -eq $portgroup
  Write-Host found $portgroup with vmkernel $vmk on $vmhost -ForegroundColor yellow
  Write-Host removing vmkernel $vmk on $vmhost -ForegroundColor Green

  #removing vmkernel
  Remove-VMHostNetworkAdapter -Nic $vmk -confirm:$false
 
 }
}

 Okay now that we deleted the vmkernel $vmk from the portgroup $portgroup, let us delete the portgroup too.


foreach ($vmhost in $vmhosts)
{
$pgs       = get-vmhost $vmhost | get-virtualportgroup | select name
if ($pgs -match "VMkernel")
 {
  $vmk = Get-VMHostNetworkAdapter -VMHost $vmhost | where PortgroupName -eq $portgroup
  Write-Host found $portgroup with vmkernel $vmk on $vmhost -ForegroundColor yellow
  Write-Host removing vmkernel $vmk on $vmhost -ForegroundColor Green

  #removing vmkernel
  Remove-VMHostNetworkAdapter -Nic $vmk -confirm:$false

  #removing portgroup
  get-virtualportgroup -Name $portgroup | Remove-VirtualPortGroup -Confirm:$false
 }
}

Deleting a regular vm portgroup is very easy as mentioned here with just an one liner but it would be nice if we can have a common script where you can just delete all kind of portgroups with or without vmkernel....
I will keep this updated here https://github.com/gajuambi/vmware


No comments:

Post a Comment