Re-Registering VMs and Reconfigure vDS-Portgroups Removed from Inventory

Post date: Jul 22, 2013 1:31:08 PM

Big Thanks to for this one! I wrote this Add-on to better support environments with Distributed Switches.

The cache module on the array controller failed on a host, and ESXi was wasted. The host stopped responding in vCenter, but did not fail hard enough to trigger HA. Most of the VMs on it were still up and their OSes were responsive to ping requests. Eventually we had to remove the host from vCenter. At that point, all VMs registered on it also are removed from inventory.

I grabbed the info for the guests that were in "disconnected" state _BEFORE_ removing the affected host from vCenter, then removed the host, then re-registered VMs and started them.

However this is great for non vDistributed Switch environments. When the bad host is removed, the orphaned VMs lose their Network Adapter vDS-PortGroups when added back to inventory. This will collect the network information and reconfigure the affected VM's Network Adapters with their appropriate PortGroups. Hope this helps people out there!

## Script function: Re-register VMs after host removal from vCenter and re-apply VM Portgroups

## Author:

## Distributed Switch bits by KnightUSN

## July 2013

## cluster in which troubled host resides

$strAffectedCluster = "myCluster"

## get array BEFORE removing bad host from vCenter!!! Else, will not be able to get this info in this manner, since disconnected VMs are removed from inventory at that time, too

## get an array of VM .Net View objects where their OverallStatus is "gray"; optimized for low memory consumption

$arrVMsToHandle = Get-View -ViewType VirtualMachine -SearchRoot (Get-Cluster $strAffectedCluster | Get-View).MoRef -Property Name,Parent,LayoutEx.File,Summary.OverallStatus -Filter @{"Summary.OverallStatus" = "gray"}

## Get Network adapters and PortGroups per VM

$arrVMnets = Get-VM $arrVMsToHandle.Name | Get-NetworkAdapter | Select Name,NetworkName,@{N="VM";E={$_.Parent.Name}}


##Remove the affected Host(Power Off and Remove from vCenter) and verify HA failed before continuing.


## get the hosts in the cluster, to be used for re-registering VMs

$arrHostsInAffectedCluster = Get-Cluster $strAffectedCluster | Get-VMHost

## For each set of VM name, parent folder ID, and .VMX file path, add VM to inventory via New-VM cmdlet; add to random host within the cluster

$arrVMsToHandle | Select Name, Parent, @{n="vmxFilePath"; e={($_.layoutex.File | ? {$ -like "*.vmx"}).name}} | %{New-VM -VMFilePath $_.vmxFilePath -VMHost ($arrHostsInAffectedCluster | Get-Random) -Location (Get-VIObjectByVIView $_.Parent) -RunAsync} ## end ForEach-Object

## start all of the VMs (may cause a boot storm if huge number of VMs -- split up by groups of x VMs as necessary)

$arrVMsToHandle | %{Start-VM $_.Name -RunAsync}

##Reconfigure VM Nics with PortGroups

ForEach($Adapter in $arrVMnets){Get-VM $Adapter.VM | Get-NetworkAdapter | Where{$_.Name -like $Adapter.Name} | Set-NetworkAdapter -NetworkName $Adapter.NetworkName -Confirm:$false}