VMware Cloud Community
lamw
Community Manager
Community Manager
Jump to solution

Commandline for removing VMs from "Inventory"

I hope this is possible, but I haven't found a method of removing a Virtual Machine from inventory via the commandline. The reason I'm interested in this command is that I can unregister a VM from the commandline using

vmware-cmd -s unregister /path/to/vmx

and then manually delete the VM directory, the problem is on the MUI (VI Client or Virtual Center) the VM is left orphaned. I'm currently testing ability of sharing a single VMDK across the same ESX Host and by accidently removing from disk, it removes the linked reference of the VMDK. Removing from inventory is safer and you can remove the disk files manually, the problem is I'm creating dozen of VMs at once and it would be nice to be able to run a single commandline to do the same without the affects of "removing from disk"

I've tried to look at some of the vmware-vim-dm/vimsh commands and I haven't been able to find anything that will remove it from inventory.

Any help would great, thanks in advanced.

0 Kudos
1 Solution

Accepted Solutions
admin
Immortal
Immortal
Jump to solution

With the command: vmware-cmd -s unregister <vm-cfg-path>

You'll need to restart the services mgmt-vmware and vmware-vpxa in-order for the front-end to pickup the change.

Or,

The inventory is stored in the file /etc/vmware/hostd/vmInventory.xml

I suppose you could try deleting the relevent entry then restarting the mgmt-vmware and vmware-vpxa services.

I haven't tried this so I don't know if it works.

Or,

vimsh appears to have a command called vimsvc/unregister

Not sure what it does but it sounds promising.

Message was edited by: appk

View solution in original post

0 Kudos
18 Replies
kjb007
Immortal
Immortal
Jump to solution

Use powershell vi toolkit. Simple one-two line script.

Get-vm vm_to_remove |remove-vm

vExpert/VCP/VCAP vmwise.com / @vmwise -KjB
0 Kudos
lamw
Community Manager
Community Manager
Jump to solution

I do not want to delete from disk, please read the posts carefully. This will automatically remove the disk it references since we use Virtual disk-sharing. I would like to be able to issue this via the commandline of the ESX Host or through other remote utilis like powershell. My preference is the service console using traditional VMware commands or vimsh, if that is impossible, we can install the powershell toolkit on our Virtual Center server and issue the command. Thanks

0 Kudos
admin
Immortal
Immortal
Jump to solution

With the command: vmware-cmd -s unregister <vm-cfg-path>

You'll need to restart the services mgmt-vmware and vmware-vpxa in-order for the front-end to pickup the change.

Or,

The inventory is stored in the file /etc/vmware/hostd/vmInventory.xml

I suppose you could try deleting the relevent entry then restarting the mgmt-vmware and vmware-vpxa services.

I haven't tried this so I don't know if it works.

Or,

vimsh appears to have a command called vimsvc/unregister

Not sure what it does but it sounds promising.

Message was edited by: appk

0 Kudos
lamw
Community Manager
Community Manager
Jump to solution

I'll try to look at the vmInventory.xml and restart the services, I've already tried the vimsh command unregister is the same as vmware-cmd -s unregister which will remove from ESX Inventory, but that automatically orphans VM on the client. I've tried to restar the client and restart the managment services, no go. Hopefully the vmInventory.xml is I can tweak on the fly. I'll let you know

0 Kudos
admin
Immortal
Immortal
Jump to solution

When I unregister a VM from the command line, it gets removed from the front-end of ESX but not from VirtualCenter.

i.e. if you are connected directly to the ESX using the VI Client, it will update correctly.

In VirtualCenter it shows as orphaned. Is this what your referring to?

This happens because the VC database is not aware of the unregister because the command was issued on the ESX server.

There is no command on ESX to unregister a VM through VirtualCenter but you can use the VI Perl Toolkit or one of the other API's.

The VI Perl Toolkit comes with a script for unregistering VM's through VirtualCenter:

http://www/support/developer/viperltoolkit/viperl15/doc/vmregister.html

This worked for me with: perl vmregister.pl --url https://vchost/sdk/vimService --username administrator --password blah --operation unregister --vmname TESTVM

0 Kudos
kjb007
Immortal
Immortal
Jump to solution

The software messed up the command line. The last bit about deletefromdisk is optional. The command by itself will remove from inventory.

-KjB

vExpert/VCP/VCAP vmwise.com / @vmwise -KjB
0 Kudos
lamw
Community Manager
Community Manager
Jump to solution

Actually, when I unregister the VM from the commandline on the ESX Host, the changes remove the VM from the internal VM Inventory, meaning if you did a "vmware-cmd -l" it will not be shown ... but this does not reflect on the VI Client directly connecting to ESX Server. And, yes you're right it does not take affect on VC either. So I'm not sure how you're doing it, but I still get an orphaned VM on the ESX Host via the VI Client. I have not had a chance to play with the vmInventory.xml configuration, but perhaps initiating the removal via VC through the script you provided might be one approach. For now, this is really just testing purposes and it's occuring on 1 ESX Host that's not connected to VC, so it would be really nice to be able to do this through the service console. So far from the posts, it looks to be impossible, least with the traditional commands other than going to the VI Toolkit via perl or powershell

0 Kudos
lamw
Community Manager
Community Manager
Jump to solution

So I remove the reference in /etc/vmware/hostd/vmInventory.xml and restarted the management services and that seems to do the trick, though the problem with testing this directly connected to the ESX Server is when you bounce the mgmt-vmware service, you need to re-login to the ESX Host, but it does work. Once this has been verified in VC, we just might see quick disconnect from the ESX Host which is more tolerable than having to re-connect to the VI Client. If that is the case, then I guess this is the quickfix, I would still like to explore the rest of the "vmware-vim-cmd" since theres been more additional commands and maybe theres a cleaner way of doing it via the commandline, if not, we can also look at powershell, though at this point, we're not utilizing powershell and would like to limit the amount of tools that gets installed on our admin servers or on VC Server. Thanks for your help, I think your solution was the one I was looking for.

0 Kudos
lamw
Community Manager
Community Manager
Jump to solution

Update: I actually just tried the vimsh command "vmware-vim-cmd vmsvc/ungregister &lt;VM_ID&gt;", it actually unregistered the VM and it removed from Inventory and we don't see the orphan status anymore, I swear I had done this before, but maybe I got that confused with the traditional vmware-cmd -s unregister, I'll have to play with it alittle more, but I think this might be the best solution and then afterwards remove the directory so I remove the test VM but the refereneced .vmdk does not get removed. I'll have to add some logic to pull out the internal ID from within the "vimsh" but that won't be too bad. Wish I could double your award points! =]

0 Kudos
JasonW
Contributor
Contributor
Jump to solution

If "vmware-vim-cmd vmsvc/ungregister &lt;VM_ID&gt; worked for the orphaned VM, how do you get the VM_ID of a machine in an orphaned state, as it would not be powered on?

vm-support -x will only show the vmid of running VMs.

0 Kudos
lamw
Community Manager
Community Manager
Jump to solution

Actually to get the vmid which is different from vm-support -x, you need to loop through or grep for the known name through "vmware-vim-cmd vmsvc/getallvms", this will least out all VMs with about 5-6 columns, you'll need to awk out column 1 which is going to be your VMID. So if you know which VMs or set of VMs you either grep for a specific VM or just for loop through them all and pipe that into unregister.

0 Kudos
JasonW
Contributor
Contributor
Jump to solution

Thanks for the info, I was able to get the vmid of the machine. However the 'vmware-vim-cmd vmsvc/unregister &lt;vm_id&gt;" command still left the VM in a orphaned state.

0 Kudos
lamw
Community Manager
Community Manager
Jump to solution

After your question, I realized the way I was able to remove the VM inventory successfully w/o leaving the VM in an orphan state was just to loop through the VMs or specific VMs in "vmware-cmd -l" and do "vmware-cmd -s unregister" this would actually remoe the inventory and not create orphan VMs, then I would delete the actual path to the VM. This was part of a purge script, and really how my question was posted intially. Give that a try and hopefully it works, all my tests have been on ESX 3.5,3.5u1 and 3.5u2

0 Kudos
JasonW
Contributor
Contributor
Jump to solution

Thanks for your help but this still does not work for me, and leaves the VM in an orphaned state in the VI client. I am running ESX3.5 u2. I have tried this on another VM and has the same result.

I may open a support call with VMware and see what they suggest.

0 Kudos
lamw
Community Manager
Community Manager
Jump to solution

Hm interesting, technically by unregistering your VMs before deletion should not orphan your VMs. The script I have running is also on 3.5u2 Hosts, the script allows you to create set of VMs in our case they're lab systems and the junk of code that allows you to quickly remove a set of VMs basically ensures they're powered off and then does an unregisteration using "vmware-cmd -s unregister" and then deletes them from their stored location on the VMFS.

We're using: VMware ESX Server 3.5.0 build-110268

0 Kudos
JasonW
Contributor
Contributor
Jump to solution

I totally agree that technically what I am doing should work. I have a support call open with VMware now and will update this thread with any new developments.

0 Kudos
JasonW
Contributor
Contributor
Jump to solution

After speaking with VMware technical support we ended up removing the VM from the SQL database. This removed it from the inventory in the VI client.

0 Kudos
Vrunner
Contributor
Contributor
Jump to solution

Are you connecting directly to the hosts with the VI Client, or through Vcenter?

What I am seeing is that connecting directly to the host, the "vmware-cmd -s unregister" does in fact remove it from inventory, while if you check Vcenter the guest is orphaned. I've restarted management services, rebooted the host, and even disconnected it from vcenter and readded. The only way I've been able to get Vcenter to see the guest as really gone is to remove the host, delete it and then re-add it as a new host... or simply remove the orphaned guest from inventory - unfortunately both options void the whole "automation" theory.

Have there been any new thoughts on this or any fixes? We are running 3.5 U4, Client and Vcenter 2.5 (builds 104215 and 147633 respectively).

We, too, are trying to get this functioning without PERL or PowerShell.

0 Kudos