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.

4 thoughts on “PowerCLI One-Liner – Mark all VM optical drives as Client

  1. Get-VM * | Get-CDDrive | where { $_.IsoPath -or $_.HostDevice -or $_.RemoteDevice -ne $null} | Set-CDDrive -NoMedia -Confirm:$false

  2. Cool one-liner. But it’s potentially risky since a mounted CD/ISO may well be in use by the VM!

    I use a version of this to set the drive to “Client Device” only if there’s it’s not currently connected. Those are always safe to remove, imo.

    • If it is in use, then you should know that from the person working on that VM. if not then you need proper change management in place to be aware of other people working in your environment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s