Finding orphaned Virtual Machines – The Python Way

For the past few years I’ve done a little dabbling in Powershell for on-demand automation. I wanted to expand my horizons outside the realm of Powershell and find something more powerful, not only for vSphere, but other technologies that have APIs accessible via Python. I had never used Python, but had to start somewhere. I decided to start with Learning Python the Hard Way. I really enjoyed this book. The entire book is comprised of exercises. A lot of the exercises the author just has you complete them, without explanation. Don’t let that intimidate you. The author explains everything as you go through the book. Once I got a basic understanding, and I use the term basic very loosely as I still need to cement my foundation, I needed to figure out how to use Python to access the vSphere Web Services API.

I needed to find out how exactly to access the API with Python and found a good article by William Lam about how to install pyVmomi (used to access the vSphere API via Python). You can find the article here. Ok, now I have pyVmomi installed on my system, now what? There are a few sample scripts for how to connect to vCenter or an ESXi host using pyVmomi located on Github, you can find those in the pyVmomi repository on Github. Using the sample script I was able to see how the connection was done and get some basic information. Pretty cool so far.

Now, to find a problem to solve. In the past I’ve accidentally found virtual machines that were still on datastores that did not exist in the inventory and I thought this was a great place to start my Python quest. Virtual machines that live on datastores that are not in the inventory is the result of one of three scenarios:

  1. Someone accidentally removed the virtual machine from the inventory
  2. Someone “deleted” the virtual machine, but actually, they only removed it
  3. Someone wanted the virtual machine out of the inventory, but wanted to keep the files

Number three is the only valid reason to have virtual machines residing on datastores but not in the inventory. Within vCenter there isn’t any report or any easy way to see virtual machines that exist on datastores but aren’t part of the inventory. I decided this was going to be my first python project. So now that I had my problem I could start to solve it with Python. As I mentioned earlier, the sample script showed me how to connect to a host or vCenter and start pulling data, but I needed a reference to show me all the different objects and properties. The API reference is here. I could now connect to my vCenter server using python and I had this cool reference to guide me through the different areas and get the data I need. Sounds good, right? Well, not really. I got 10 minutes into it and was stuck. There were certain methods I needed to call to search the datastores and I had no idea what I was doing. I turned to Twitter. I got a few responses from Anthony Elizondo, Matt Brender and Tim Jabaut stating they wanted to get involved. I started a Google+ community for us to collaborate. Here is where Anthony was able to fill in the pieces I was missing in order for me to continue (along with a lot of other useful information).

It’s been a few weeks, but I’ve finally finished the script (below). It’s had limited testing so I encourage you to download it and give it a go. You’ll need to run the script with three parameters, here is an example (each parameter has two dashes in front):

python –host <hostname/IP> –user –password  


Here is an example of the output:


I’d like to especially thank Anthony for taking the time to help me out. I’m looking forward to expanding my Python and working with Matt, Anthony and Tim.

Leave a Reply

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