After a failure or event with data loss, its possible for vCenter to lose a VM. When this happens, the VM is marked as orphaned. Orphaned VMS will exist as a stub in vCenter and you can right click and remove them from Inventory, but getting a list of these VMs prior to making changes is a great first step. PowerCLI is a great way to capture a full snapshot of that information before making changes.
[code language=”PowerShell”]$OrphanedVMs = Get-VM * | Where {$_.ExtensionData.Summary.Runtime.ConnectionState -eq "orphaned"}[/code]
By storing the full VM information in a variable, you can save it for reference.
Modify the cmdlet slightly and you can automate removal of Orphaned VMs, rather than having to right click each one in vCenter and select Remove From Inventory.
[code language=”PowerShell”]Get-VM * | Where {$_.ExtensionData.Summary.Runtime.ConnectionState -eq "orphaned"} | Remove-VM[/code]
The above cmdlet will prompt you one by one to remove the VM from inventory, or you can select the Yes to All option on the first prompt.
How do you get Orphaned VMs?
vCenter marks a VM as orphaned when vCenter has the VM’s ID in its database, but the location of the VMX file is no longer where vCenter believes it is located. This does not mean the VM doesn’t exist on a file system somewhere. By using the PowerCLI above, you get the full details of the VM, including the location where vCenter believes its VMX is located.