In one of my previous posts(https://techibee.com/powershell/how-to-connect-iso-image-file-to-virtual-machine-in-hyper-v-remotely-using-powershell/2306), I showed how to connect a ISO file to Virtual Machine in Hyper-V using PowerShell. This post will help you to identify and remove ISO file from a virtual machine. This works for all versions of Hyper-V(Windows Server 2008 & Windows Server 2012).
If you are just worried about managing VMs in Windows Server 2012 based Hyper-V, then all you need is just a single line. Set-VMDVDDrive cmdlet helps you disconnect a ISO or connect a ISO file. If you are on Windows server 2008, then you need this script. This is actually stripped from my previous post where I am connecting ISO using powershell & WMI.
Windows Server 2012 Hyper-V
Import-Module Hyper-V Set-VMDvdDrive -Path $null -VMName Win8
In case if your multiple DVD drives and you want to clear only one of them, then you need to specify the controller ID. Controller number and Controller location you can get from VM properties in Hyper-V Console.
Set-VMDvdDrive -Path $null -ControllerNumber 1 -ControllerLocation 1 -VMName Win8
Windows Server 2008 Hyper-V & Windows Sever 2012 Hyper-V
[cmdletbinding()] Param( [string]$VMName, [string]$HyperVServer = $env:ComputerName ) $VMMS = Get-WMIObject -Namespace root\virtualization ` -Class Msvm_VirtualSystemManagementService ` -ComputerName $HyperVServer ` -EA Stop # Get the virtual machine object $VM = Get-WMIObject -Namespace root\virtualization ` -Class MSVM_ComputerSystem ` -Filter "ElementName='$VMName'" ` -ComputerName $HyperVServer ` -EA Stop # SettingType = 3 ensures that we do not get snapshots $SystemSettingData = $VM.getRelated("Msvm_VirtualSystemSettingData") | where {$_.SettingType -eq 3} # Get the first DVD drive in the system $DVDDrive = $SystemSettingData.getRelated("Msvm_ResourceAllocationSettingData") | where{$_.ResourceType -eq 16} | select -first 1 # Make sure the DVD actually exists if ($DVDDrive -eq $null) { Write-Warning "No DVD drive exists on that virtual machine" return } # Check to see if there is already media in the DVD drive $existingRASD = $SystemSettingData.getRelated("Msvm_ResourceAllocationSettingData") | where {$_.Parent -eq $DVDDrive.__Path} # If there is media in the drive - eject it if ($existingRASD) { $result1 = $VMMS.RemoveVirtualSystemResources($VM, @($existingRASD)) if($result1.ReturnValue -eq 0) { Write-Host "Successfully ejected the exiting ISO image" } } else { Write-Host "No ISO file is currently connected" }
Output:
Hope this helps and happy learning.
Comments on this entry are closed.
$clusterNodes = Get-ClusterNode | select Name
ForEach($node in $clusterNodes) {
$VMList = @()
$VMList = Get-VM -ComputerName $node.Name
ForEach($vm in $VMList) {
#Set-VMDvdDrive -Path $null -ComputerName $node.Name -VMName $vm.Name
$ISOImage = Get-VMDvdDrive -ComputerName $node.Name -VMName $vm.Name
If ($ISOImage.DvdMediaType -ne “None”) {
Set-VMDvdDrive -Path $null -ComputerName $node.Name -VMName $vm.Name
$ISOImage.VMName + ” – DETACHED”
}
}
}