ESXi 5.x: Registering / unregistering VMs from the CLI in one go.


I theory, this can be done without the CSV, using this command:

vim-cmd solo/registervm /vmfs/volumes/……MY_VM.vmx MACHINE_NAME cat /etc/vmware/hostd/pools.xml | grep "YOUR-RESOURCE-POOL-NAME" -A1 | grep "[objID]" | sed 's///;s/<\/objID>//g' | sed -e 's/^[[:blank:]]*//;s/[[:blank:]]*$//' vmware-vim-cmd solo/registervm /vmfs/volumes/olga-local-SAS.Storage/UCSB-GAUCHOS/UCSB-GAUCHOS.vmx UCSB-GAUCHOS cat /etc/vmware/hostd/pools.xml | grep "UCSB-DEV-RP" -A1 | grep "[objID]" | sed 's///;s/<\/objID>//g' | sed -e 's/^[[:blank:]]*//;s/[[:blank:]]*$//'
But I wanted to use a CSV file for practice. The CSV file contains two columns: col1 = vmname, col2 = Resource pool. Unfortunately the command below doesn’t work using the name seen in the client. It requires the objID of the Resource pool. To get the resource pool’ objID, run the following command:
cat /etc/vmware/hostd/pools.xml | grep “Resource Pool Name” -A1 | grep “[objID]”
All my VM names correspond to their directory and .vmx name. I am yet to see an ESX install that doesn’t work this way. Using the above command, we can create a csv that looks something like:
1.  Make and save the file on the ESXi host. e.g. register.csv (copy from Excel and paste into VI worked for me).
2. cd to the datastore containing the VMs. You could add an additional column with the full path if you VM’s are all over the place.
3. enter the following
vim-cmd usage:
Usage: registervm vm path [name] [resourcepool]
registervm [cfg path] [name(optional)] [resourcepool(optional)]
Passing a CSV:
No. 18.
Answering VM related questions from the command line:
To remove all those Unknown(x) VM’s in one go.
1. Get a list of all the invalid VMs:
# vim-cmd vmsvc/getallvms
2. Confirm the ID numbers are in sequential order, finding the min and max values and no valid VM ID’s fall in the range. Don’t worry if there is a missing number in the sequence.
3. Create a While loop to count through:
# while [ $COUNTER -lt 56 ]; do
> vim-cmd vmsvc/unregister $COUNTER
This will loop through, removing any “Unknown” VM, according to the ID.
If you did inadvertently include a required VM, it can always be added back. These commands do not actually delete anything.

Please feel free to leave a comment...