Six Million Ways To…
  • About
Powercli , Scripts

vCenter : Remove orphaned VMs

by Axel September 28, 2017 No Comments

A VM appears as  orphaned  in the vCenter inventory when it has been directly deleted from the inventory of the host (in a vSphere-client session) instead of the vCenter one.

If the VM is not needed anymore the inventory must be cleaned so the vCenter database can be up-to-date.
Manually remove an object  is simple but it can really get boring  when it comes to do the same for several objects.

​Among many, one method to remove orphaned VMs from vCenter inventory :

clear

$vcenter = read-host “Please enter the FQDN of the vCenter you want to connect to”
write-host “vc” $vcenter -ForegroundColor cyan
Connect-VIServer $vcenter

 

write-host “Looking for orphaned VMs…” -ForegroundColor Gray

$OrphanedVM = @(get-vm |get-view | ?{$_.summary.runtime.connectionstate -eq “orphaned”})
if(!$OrphanedVM)
{write-host “No orphaned VM found,leaving…” -ForegroundColor Gray;break}

 

$userAnswer = read-host (“You are about to delete the following VM ” + $OrphanedVM.name + ” : . Do you confirm (y/n) ?”)
if ($userAnswer.ToLower() -eq “y”)
{
foreach($VM in $OrphanedVM)
{
$VMName = $VM.name

try
{
write-host “Removing VM $VMName…” -ForegroundColor Gray
get-vm $VMName | Remove-VM $VM -Confirm:$false
write-host “VM VMName successfully removed” -ForegroundColor Green
}
catch
{
write-host (“Failed to remove VM $VMname. Error : `n” + $_.Exception.message) -ForegroundColor Red
}
}

}
else
{
write-host “Leaving…”
}

vCentervSphere

  • Previous ESXi : Configuring network via CLI5 years ago
  • Next vRO : Reminder : How to call or initialize objects, actions.5 years ago

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Menu

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Recent Posts

  • vRA : API : Submit a request
  • vRO: Setting constants for workflows
  • VM : Quickly delete one or more VM
  • vRO: Using vRA REST API to get all vCAC VMs
  • vRO : Workflow to Get Restarted VMs by HA

Archives

  • October 2019
  • March 2019
  • March 2018
  • September 2017

Categories

  • CLI (2)
  • ESXi (1)
  • Orchestrator (4)
  • Scripts (3)
    • Powercli (3)
  • vRA (2)
  • vSphere (1)

Tags

API (1) CLI (1) HA (1) Nutanix (1) PowerCLI (1) Reminder (1) REST (1) REST API (1) vCenter (3) VRA (2) VRO (3) vSphere (4)
2023 Six Million Ways To…. Donna Theme powered by WordPress