How to list mounted ISO's and unmount them from the command line in VMware ESXi?

Solution 1:

A quick & crude solution on the shell via SSH would be to write a small script that connects several actions:

  1. Get your VM IDs with vim-cmd vmsvc/getallvms|awk '{print $1}'|grep -o -E '[0-9]+' (list all VMs, only show first column with awk, filter out IPs and text and empty lines with grep)
  2. Iterate over the lines (ash does not have arrays like bash) and check for each number/ID if one occurrence of your chosen ISO name is found in the device listing for each VM: vim-cmd vmsvc/device.getdevices yourVmId|grep -o -A 12 -E 'yourImageName.iso' | grep -c 'connected = true' (list all devices, get the area around your ISO file, check if the ISO is currently mounted/active)
  3. Get the device ID of each CD drive (assuming it is only one, change the code for multiple drives on a single machine) with a modified grep from the same initial listing: vim-cmd vmsvc/device.getdevices yourVmId|grep -o -B 4 -E 'yourImageName.iso'|grep -o -E 'key = [0-9]+'|grep -o -E '[0-9]+' (double grep is neccessary because of missing group option -P)
  4. Use vim-cmd vmsvc/device.connection yourVmId yourDeviceId disconnect to disconnect the device. (Edit: It seems that this is not entirely correct, it does something, but not what I would expect. I'll update when I have time to investigate this further)

The only problem I encountered is that the message "CD drive locked by guest" may appear in the VSphere client while doing the last step, but maybe this can be disabled generally.

Solution 2:

Use PowerCLI...

Or just do it manually for the "several" VMs.
Or avoid the situation in the first place by unmounting after installation.