Page tree
Skip to end of metadata
Go to start of metadata

While having an ISO mounted to a Hyper-VM doesn't routinely cause an issue, keeping the vmguest.iso file mounted after the Hyper-V Integration Services have been installed can result in the file being locked on the Hyper-V host, which can result in a large number of temporary files accumulating on the Hyper-V host. It is recommended to install the Hyper-V Integration Services on new VMs and then eject the vmguest.iso file from them to prevent this from happening.

It's easy enough to eject an ISO from a single VM, but a modern virtualised infrastructure may have from tens to hundreds of VMs. Fortunately using PowerShell it's very easy to eject mounted ISOs from all VMs on one or more Hyper-V hosts, using the script below

$vmhosts = "HOST1","HOST2","HOST3"
foreach ($vmhost in $vmhosts)
{
    Write-Host "Get VMs on host " $vmhost
    $vms = Get-VM -ComputerName $vmhost
    
    foreach ($vm in $vms)
    {
        Write-Host "Get drive properties for VM " $vm.VMName " on host " $vmhost
        $vmdrive = Get-VMDvdDrive -VM $vm
        if ($vmdrive.DvdMediaType -eq "ISO")
        {
            Get-VMDvdDrive -VM $vm | Set-VMDvdDrive -Path $null
        }
    }
}

This script will retrieve the list of VMs running on each of three Hyper-V hosts; HOST1HOST2 and HOST3. The $vmhosts variable should be modified to contain a list of the Hyper-V host servers in your environment. The intention would be that each individual host should be added here, however the script could be extended to automatically retrieve the list of hosts in a cluster using the Get-ClusterNode cmdlet.

The script assumes that each VM will have exactly one DVD drive and will check that the mounted media is an ISO file before ejecting it.