Tuesday 20 January 2015

PowerCLI and CD Drives

We were doing a bit of housekeeping today.  We've already moved our vSphere Centre from 5.0 to 5.5 and it's time to update the hosts.  That should be straightforward; each host should be put into maintenance mode while ESXi is updated then the host is brought back into the cluster.  One possible problem though, is VM CD Drives.

I'm terrible for not managing VM CD Drives properly.  I have an annoying habit of uploading an ISO to a host's local storage and making it available to a VM.  Or, I'll stick a CD in the host and pass it through to a VM.  These are both really bad things to do in VMware.  When a VM has a CD Drive attached to an ISO stored locally on the host or to the host CD device then it cannot be vmotioned.  That means when the host goes into maintenance mode, you're going to have a problem.  The best thing to do is to check the CD drives first.  The easiest way to do it is in PowerShell with PowerCLI.



VMware PowerCLI is, according to VMware, "a Windows Powershell interface to the VMware vSphere and vCloud APIs".  This means we can perform operations from within PowerShell that we would normally do from the vSphere GUI.  Sometimes it's quicker and simpler to use the GUI but we can automate from PowerShell.  When dealing with a lot of objects, that can be a huge time saver.

Back to my original problem - finding any VMs that have a CD Drive connected.  Once you have PowerCLI running and you've connected to a vCenter (Connect-VIServer servername) We can start with:

Get-Command *CD*

Sensible place to start.  This will show us all commands that include the term CD.  This actually returns 39 commands, many of which are not relevant, but one looks very promising.  Get-CDDrive.



That's what I'm looking for.  The syntax shows me I can give it a VM name and find CD drives on that VM.


Nothing attached here.  If I pipe that command through Format-List, I can see all attributes that are available to me.


I'm interested in the Parent, which is the name of the VM that the CD Drive is connected to and the ConnectionState of the CD Drive.  Using both those parameters I can see which VM I have and the state of any CD Drive attached.  It would be really useful to see that for all VMs at once though.

Get-VM without any parameters returns an array of objects, each object being a VM in your vCenter.  


Trust me, there are VMs under there.  That means, I can pipe those results to our earlier Get-CDDrive command and add a little formatting to select the Parent and ConnectedStat properties to get a nice list!


After all that, I was worrying about nothing, everything showing as NotConnected.  Still, at least now I can upgrade with confidence!