Friday, March 29, 2013

PowerCLI - Removing inaccessible VM's from inventory

In my lab I often destroy and re-create Nutanix clusters which sometimes leaves stale, inaccessible datastores as well as a bunch of inaccessible VM's. The following script will remove all these inaccessible VM's from inventory (Thanks to my colleague Steven for this one):
Get-Cluster -Name $clustername | Get-VM | Get-View | Where {-not $_.Config.Template} | Where{$_.Runtime.ConnectionState -eq “invalid” -or $_.Runtime.ConnectionState -eq “inaccessible”} | %{Remove-VM -VM $_.Name}

3 comments:

  1. Hi

    Just found this.....saved me a load of time! Much appreciated.

    Danny

    ReplyDelete
  2. Thank you for posting this, it worked for me! Had a NFS datastore I couldn't unmount and after this I was able to unmount.

    ReplyDelete
  3. Thank you very much for saving me from a headache. There was a deleted machine in the VM cluster which was hanging out as inaccessible, this command solved my problem.

    ReplyDelete