Remove all Connected CD Drives and IsoPaths

The following script gets all the VMs that have connected CD ROM drives and in addition it also picks up VMs that may not have CDs in a connected or connect at power on state, but do have something in the IsoPath. The reason I like to remove the IsoPath data as well is that I have a report that I run that lists the datastores that VMs are on. If a VM has data in the IsoPath for the CD ROM this is picked up by the report, which is not what I am after. That and it makes the environment nice and tidy. 🙂

#Create a list of VMs with CD drives connected or have data in the IsoPath
$vmlist = Get-VM | ? { $_ | Get-CDDrive | ? { $_.ConnectionState.Connected -eq $true -or $_.IsoPath -ne $null } } | Sort
#Set CD Drives to Client Device, removing the Connected status and deleting the IsoPath
$vmlist | Get-CDDrive | Set-CDDrive -NoMedia -Confirm:$false

The same can be done for Floppy drives as well.

#Create a list of VMs with CD drives connected or have data in the IsoPath
$vmlist = Get-VM | ? { $_ | Get-FloppyDrive | ? { $_.ConnectionState.Connected -eq $true -or $_.FloppyImagePath -ne $null } } | Sort
#Set CD Drives to Client Device, removing the Connected status and deleting the IsoPath
$vmlist | Get-FloppyDrive | Set-FloppyDrive -NoMedia -Confirm:$false

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.