Script: How to check and remove CD Drive or ISO Connection of a VM using Powershell or PowerCLI – VirtuallyThatGuy

This is a quick script for reporting and taking actions on how to check and remove CD Drive or ISO Connection of a VM using Powershell or PowerCLI. This is very useful when system admin have mounted an ISO to a VM using local datastore and vmotion not migrating the VM due to the connected ISO file.

CD Drive or ISO connected Reporting

 

 # Check VMs with CD ROM attached to them

get-vm​​ |​​ where​​ {​​ $_​​ |​​ get-cddrive​​ |​​ where​​ {​​ $_.ConnectionState.Connected​​ -eq​​ "true"​​ } }​​ |​​ select​​ Name

# Check VMs with ISO files attached to Them

get-vm​​ |​​ where​​ {​​ $_​​ |​​ get-cddrive​​ |​​ where​​ {​​ $_.ConnectionState.Connected​​ -eq​​ "true"​​ -and​​ $_.ISOPath​​ -like​​ "*.ISO*"} }​​ |​​ select​​ Name,​​ @{Name=".ISO Path";Expression={(Get-CDDrive​​ $_).isopath }}

 # Disconnect all VMs where the CD Drive is connected and it is​​ NOT​​ an .ISO

$VMs​​ =​​ Get-VM

$CDConnected​​ =​​ Get-CDDrive​​ $VMs​​ |​​ where​​ { ($_.ConnectionState.Connected​​ -eq​​ "true")​​ -and​​ ($_.ISOPath​​ -notlike​​ "*.ISO*")}

If​​ ($CDConnected​​ -ne​​ $null) {Set-CDDrive​​ -connected​​ 0​​ -StartConnected​​ 0​​ $CDConnected​​ -Confirm:$false​​ }

# Disconnect all VMs where the CD Drive is connected and it is an .ISO

$VMs​​ =​​ Get-VM

$CDConnected​​ =​​ Get-CDDrive​​ $VMs​​ |​​ where​​ { ($_.ConnectionState.Connected​​ -eq​​ "true")​​ -and​​ ($_.ISOPath​​ -like​​ "*.ISO*")}

If​​ ($CDConnected​​ -ne​​ $null) {Set-CDDrive​​ -connected​​ 0​​ -StartConnected​​ 0​​ $CDConnected​​ -Confirm:$false​​ }

# VMs with a mounted ISO file as a CD/DVD drive

Get-VM​​ |​​ FT​​ Name,​​ @{Label="ISO file"; Expression​​ =​​ { ($_​​ |​​ Get-CDDrive).ISOPath }}

 

 

# Check VMs with CD ROM attached to them
get-vm | where { $_ | get-cddrive | where { $_.ConnectionState.Connected -eq "true" } } | select Name
# Check VMs with ISO files attached to Them
get-vm | where { $_ | get-cddrive | where { $_.ConnectionState.Connected -eq "true" -and $_.ISOPath -like "*.ISO*"} } | select Name, @{Name=".ISO Path";Expression={(Get-CDDrive $_).isopath }}
 # Disconnect all VMs where the CD Drive is connected and it is NOT an .ISO
$VMs = Get-VM
$CDConnected = Get-CDDrive $VMs | where { ($_.ConnectionState.Connected -eq "true") -and ($_.ISOPath -notlike "*.ISO*")}
If ($CDConnected -ne $null) {Set-CDDrive -connected 0 -StartConnected 0 $CDConnected -Confirm:$false }
# Disconnect all VMs where the CD Drive is connected and it is an .ISO
$VMs = Get-VM
$CDConnected = Get-CDDrive $VMs | where { ($_.ConnectionState.Connected -eq "true") -and ($_.ISOPath -like "*.ISO*")}
If ($CDConnected -ne $null) {Set-CDDrive -connected 0 -StartConnected 0 $CDConnected -Confirm:$false }
# VMs with a mounted ISO file as a CD/DVD drive
Get-VM | FT Name, @{Label="ISO file"; Expression = { ($_ | Get-CDDrive).ISOPath }}

You May Also Like

About the Author: VirtuallyThatGuy

1 Comment

Leave a Reply

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