The script posted in this article will help you to mount a ISO file to a Virtual Machine(VM) in Hyper-V running on Windows Server 2012 or Windows Server 2008 R2.
While building a VM in my test lab I was mounting ISO to start the installation. I thought why not mount the ISO using PowerShell and built this script. This script works based on WMI classes in root/Virtualization namespace. So it be used in all version of Hyper-V available in market.
Using this script you can connect ISO to DVD drive in a VM running on remote Hyper-V server as well. This script ejects if there is already a ISO connected via the DVD drive and mounts the ISO you specified.
This script is based on the article written at a MSDN blog from where I grabbed the code and polished it to suite my needs.
[cmdletbinding()] Param( [string]$VMName, [string]$HyperVServer = $env:ComputerName, [string]$ISOPath ) $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" } } # Create a new object for a DVD ISO, connected to the DVD drive $DVDAllocationCapabilities = Get-WMIObject -Class Msvm_AllocationCapabilities ` -ComputerName $HyperVServer ` -Namespace root\Virtualization ` -Filter "ResourceType=21 and ResourceSubType='Microsoft Virtual CD/DVD Disk'" ` -EA Stop $DVDAllocationCapabilities = $DVDAllocationCapabilities.__Path.Replace('\', '\\') $DVDSettingsData = Get-WMIObject -Class Msvm_SettingsDefineCapabilities ` -ComputerName $HyperVServer ` -Namespace root\virtualization ` -Filter "GroupComponent='$DVDAllocationCapabilities' and ValueRange=0" ` -EA Stop $DVDSettingsData = [wmi]$DVDSettingsData.PartComponent $DVDSettingsData.Connection = @($ISOPath) $DVDSettingsData.Parent = $DVDDrive.__Path # Add the ISO to the virtual machine $result = $VMMS.AddVirtualSystemResources($VM, $DVDSettingsData.GetText(1)) if($result.ReturnValue -eq 0) { Write-Host "Successfully attached the ISO image $ISOPath to $VMName" } else { Write-host "Failed to attach the ISO image $ISOPath to $VMName" }
Hope this helps & happy learning..