PowerCLI One-Liner – Mark all VM optical drives as Client

I’ve seen a number of articles offering PowerCLI one-liners to find all VMs with connected optical drives, or set to use a datastore, or a number of other possibilities, and mark the drives as disconnected. This is helpful, but may still cause a problem with vMotions if the optical drive is pointing to a non-shared datastore ISO. vMotion does not care if the optical drive is connected, but it does care that it’s pointing to a datastore that not all hosts can see. This one-liner will find all VMs with an ISOPath set for the optical drive and reset it to Client.

Get-VM | Get-CDDrive | Where {$_.ISOPath -ne $null} | Set-CDDrive -NoMedia -Confirm:$false

This looks for all CDDrive objects where the ISOPath value is not null and calls Set-CDDrive with the flag -NoMedia. This should take care of all the VMs where someone has set it to use an ISO on the datastore. It will NOT fix the mapping for VMs connected to the Host Device or Client Device – however, those are more rare and do not usually stand between you and a vMotion. On those rare instances, you may still need to manually dismount the optical drive on the VM or use a PowerCLI one-liner targeting those settings.

Welcome to my Blog!

Welcome, one and all!

My name is Rob Nelson, and I am starting a new blog focusing on infrastructure, virtualization, automation, and security, and the intersection of these four realms. I have been working in IT for over 15 years, starting out building white box computers after high school and currently working at a managed security services provider as a sysadmin and network, security, and infrastructure engineer. The last five years have had a strong focus on building out a VMware-based virtualization platform, automating it, and providing services to our customers securely.

I’ll be passing on lessons learned, many from the school of hard knocks, in the hopes that they help you with your job and avoid some of the pain I encountered in the learning process. I hope you’ll find the posts entertaining as well! Let me know what you think in each article’s comments, or you can find me on twitter.

Enjoy!