fortypoundhead.com

Use PowerShell to Remove a Stubborn VM

Posted On 2014-08-02 by dwirch
Keywords:
Tags: Powershell Scripting Tip Windows
Views: 2472


During a recent cleanup effort in the System Center Virtual Machine Manager console, I found a powered off guest machine.  The status showed the VM as having an "Unsupported Cluster Configuration State".

The recommended action is to repair the state of the virtual machine, then attempt the operation again.  I was able to repair the state by ignoring the last operation, but I was still unable to start or remove the VM from the console.

What gives?

So I found that the VM itself was created outside of SCVMM, utilizing the Hyper-V MMC.  This created the VM with no problem, but it plopped the VM files (configuration, VHD, etc) on a local drive of the node it was created on, then moved into the cluster with with failover cluster manager.

SCVMM then discovered the machine, and noted the invalid configuration, referencing non-clustered storage.  Fine, no problem.  It won't start, so I'll delete it.  BZZZZZ! Nope!

The service account that VMM runs under apparently didn't have rights to the local path where the VM files were stored, so the job failed.  I was able to repair the error in VM, but I couldn't work with the VM in any other fashion through the VMM console.

Okey dokey, I'll simply delete the dead guest from the location in the file system, and remove it from failover cluster manager.  Well, that got me halfway.  The machine was now technically gone, but hmmm, it still is visible in the SCVMM console, even after several cluster refreshes, and I still can't remove it from SCVMM via console.

PowerShell to the rescue! 

To be honest, I wasn't sure this was going to work.  I thought maybe the VMM database was having issues, and I would have to go spelunking around the database and surgically remove the bogus entries directly.

I popped open an elevated PowerShell prompt, and typed the following command:

Remove-SCVirtualMachine -VM "My Virtual Machine Name"

I was rewarded with the a listing of the information about the VM, but no red error messages. Interesting!  So I popped over to the SCVMM console, and lo, the machine was gone.  It worked!

Moral of the story: Don't trust the GUI, but be careful with the weapon that is PowerShell.


About the Author

dwirch has posted a total of 172 articles.

 


Comments On This Post

By: AnonymousCoward
Date: 2015-09-22

~~Thanks for a good post. Sadenly, this didn't work out for me, but it did direct me to the solution.


Since I have VCenter in SCVMM too, and had another VM machine who has the same hostname, I received errors that there are duplicates and I need to be more specific. I found this post from Technet who managed to deleted my specific VM who had issue:


https://technet.microsoft.com/en-us/library/jj647747(v=sc.20).aspx

I ran this:

$VM = Get-SCVirtualMachine -VMMServer "SCVMM_HOSTNAME" | where { $_.VMHost.Name -eq "HYPER-V_HOSTNAME" -and $_.Name -eq "VM_MACHINE_NAME" }

$VM | Remove-SCVirtualMachine -Force


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:54.211.127.89

Before you can post, you need to prove you are human. If you log in, this test goes away.



Recent Forum Posts

Job Spammer: Sathya Narayana
dwirch posted on March 15, 2017 at about 7:18 in Spammers

Job Spammer: Manish Kumar
dwirch posted on March 14, 2017 at about 17:44 in Spammers

Site Migration Complete
dwirch posted on March 12, 2017 at about 11:10 in Site News

Site Migration Complete
dwirch posted on March 11, 2017 at about 0:35 in Site News

Job Spammer: Jason Weinstein
dwirch posted on March 10, 2017 at about 14:00 in Spammers

Job Spammer: Ryan Wilson
dwirch posted on March 8, 2017 at about 17:00 in Spammers