Has it happened to you that you see VMs that do not have any associated disks in certain datastores show up in those datastores, also not allowing vMotions as well leaving you confused? Well it could be that there are ISOs in that datastore mounted to those VMs! This is tedious when you are doing datastore cleanups and what not. In this post we’ll get to the bottom of creating a script that will generate a list of VMs with ISOs mounted and their paths so that you can deal with them as you wish.
There are two ways to get this, the lazy one-liner way and the a more comprehensive script. Both versions use the Get-CDDrive Cmdlet to pull the CD drive information of a VM. Lets discuss both shall we :
The lazy One-Liner
This one liner will output a csv containing ISO paths of all the VMs in the vCenter (even on the VMs without an ISO mounted to them). If you don’t mind that – this is the easiest way to get this done.
Get-VM | Select Name, @{Label="ISO file"; Expression = { ($_ | Get-CDDrive).ISOPath }} | Export-Csv "vms_with_isos.csv"
This is fine for a small vCenter with a few VMs, but is very inconvenient for a large vCenter when the output file is a few thousand lines long. So lets look at a script that will only get the VMs with ISOs mounted to them.
The Comprehensive script
$vcenter = "vcenter_name"
$vm_list_with_iso = @()
Connect-VIServer $vcenter
$vmlist = Get-VM
foreach($vm in $vmlist){
$cdinfo = Get-CDDrive $vm
if($cdinfo.IsoPath -ne $null){
$details = @{
VM_name = $vm.name
ISOPath = $cdinfo.IsoPath
}
$vm_list_with_iso += New-Object PSObject -Property $details | Select VM_name,ISOPath
}
}
$vm_list_with_iso | Export-Csv "vms_with_isos.csv"
Even though the script seems bigger, whats different is the “if” statement where it checks whether the ISOPath output by the Get-CDDrive Cmdlet is equal to $null (null variable used by powershell) or not. If its not null then that means an ISO is mounted.
And that is it!
If you liked it, please share the news! Happy Scripting!