ESXi: Removing an ISO from all running VMs is easy with this Shell script.
I use PuTTY« to connect to all my ESXi hosts. It makes light of heavy work.
View your ESXi VM, and see where the ISO file is located. In my case, it shows this: [[Dell-213] ISO/2026ToolsComplete.iso] In this code, change the [ISO PATH] to your path. Navigate to your Datastore Folder.
An easy way to find out the datastore numbers' namescd /vmfs/volumes/
Copy
Search Site
Search Google
thenls
Copy
Search Site
Search Google
This will show you all the folders.Find your Datastore that holds your ISO files.cd MyDrive-213
Copy
Search Site
Search Google
This will display your numbered name.Select your numbered name.And paste it in place of the PlaceHolder below.CD /vmfs/volumes/PlaceHolder
Copy
Search Site
Search Google
Copy and paste the following code.
[ESXi - Remove ISO from CD/DVD Rom from running VMs]
# Define the exact ISO path as shown in your CD/DVD settings="[Dell-213] ISO/2026ToolsComplete.iso"
# Loop through all VM IDsfor vmid in $(vim-cmd vmsvc/getallvms | awk '{print $1}' | grep-v Vmid); do # Check if this VM has the specific ISO mounted # We look for the backing filename in the device list=$(vim-cmd vmsvc/device.getdevices | grep-i"$ISO PATH")
if [ ! -z"$has iso" ]; then # Get the key (ID) for the CD/DVD drive holding this ISO=$(vim-cmd vmsvc/device.getdevices | grep-i"$ISO PATH"-B 15 | grep"key =" | tail-n 1 | awk '{print $3}' | sed 's/,//')
if [ ! -z"$devKey" ]; thenecho"Releasing $ISO PATH from VMID $vmid (Device Key: $devKey)..." # Disconnect the device (effectively unmounting the ISO)vim-cmd vmsvc/device.connection 0 0 0
fi
fi
done