Attaching Passthough Disks Via WMI (SCSI or IDE)
So you want to use passthough disks with Hyper-V cool – but how do you script attaching them? Well here’s your answer… But first you need to decide if you are going to attach them to the virtual IDE controller or the virtual SCSI controller. The IDE controller is ideal if you plan to boot from the passthough disk or if your guest doesn’t support the virtual SCSI adapter (Linux), the SCSI controller is ideal if you want to add/remove storage from the virtual machine while it’s running (feature of Hyper-V R2) or if you have more than 4 disks you want to add. Attaching to SCSI or IDE is pretty similar – there are really only 1 difference I bolded it on both scripts below you just have to select the correct controller. Other than that you can either use the physical disk number for disk manager (not too bad) or if you provide a LUN ID when you create the storage (in the case of a SAN) you can use that which can be a bit more deterministic. Either way enjoy!
Attaching Passthrough Disk To IDE Controller
$HyperVGuest = "Passthough Demo" $VMManagementService = Get-WmiObject -class "Msvm_VirtualSystemManagementService" -namespace "root\virtualization" $Vm = Get-WmiObject -Namespace "root\virtualization" -Query "Select * From Msvm_ComputerSystem Where ElementName='$HyperVGuest'"$VMSettingData = Get-WmiObject -Namespace "root\virtualization" -Query "Associators of {$Vm} Where ResultClass=Msvm_VirtualSystemSettingData AssocClass=Msvm_SettingsDefineState" $VmIdeController= (Get-WmiObject-Namespace"root\virtualization"-Query"Associators of {$VMSettingData} Where ResultClass=Msvm_ResourceAllocationSettingData AssocClass=Msvm_VirtualSystemSettingDataComponent" |` where-object {$_.ResourceSubType -eq"Microsoft Emulated IDE Controller"-and$_.Address -eq0 }) $DiskAllocationSetting = Get-WmiObject -Namespace "root\virtualization" -Query "SELECT * FROM Msvm_AllocationCapabilities WHERE ResourceSubType = 'Microsoft Physical Disk Drive'" $DefaultHardDisk = (Get-WmiObject -Namespace "root\virtualization" -Query "Associators of {$DiskAllocationSetting} Where ResultClass=Msvm_ResourceAllocationSettingData AssocClass=Msvm_SettingsDefineCapabilities" | ` where-object {$_.InstanceID -like "*Default"})$Disk = Get-WmiObject -Namespace "root\virtualization" -Query "select * from Msvm_DiskDrive Where DriveNumber=2" #$Disk = Get-WmiObject -Namespace "root\virtualization" -Query "select * from Msvm_DiskDrive Where ElementName Like '%Lun 200%'" $DefaultHardDisk.Parent = $VmIdeController.__Path $DefaultHardDisk.Address = 0 $DefaultHardDisk.HostResource = $Disk.__PATH $VMManagementService.AddVirtualSystemResources($VM, $DefaultHardDisk.PSBase.GetText(1)) | ProcessWMIJob $VMManagementService "AddVirtualSystemResources" |
Attaching Passthrough Disk To SCSI Controller
$HyperVGuest = "Passthough Demo" $VMManagementService = Get-WmiObject -class "Msvm_VirtualSystemManagementService" -namespace "root\virtualization" $Vm = Get-WmiObject -Namespace "root\virtualization" -Query "Select * From Msvm_ComputerSystem Where ElementName='$HyperVGuest'" $VMSettingData = Get-WmiObject -Namespace "root\virtualization" -Query "Associators of {$Vm} Where ResultClass=Msvm_VirtualSystemSettingData AssocClass=Msvm_SettingsDefineState" $VmScsiController= (Get-WmiObject-Namespace"root\virtualization"-Query"Associators of {$VMSettingData} Where ResultClass=Msvm_ResourceAllocationSettingData AssocClass=Msvm_VirtualSystemSettingDataComponent" | ` where-object {$_.ElementName -eq"SCSI Controller"}) $DiskAllocationSetting = Get-WmiObject -Namespace "root\virtualization" -Query "SELECT * FROM Msvm_AllocationCapabilities WHERE ResourceSubType = 'Microsoft Physical Disk Drive'" $DefaultHardDisk = (Get-WmiObject -Namespace "root\virtualization" -Query "Associators of {$DiskAllocationSetting} Where ResultClass=Msvm_ResourceAllocationSettingData AssocClass=Msvm_SettingsDefineCapabilities" | ` where-object {$_.InstanceID -like "*Default"})$Disk = Get-WmiObject -Namespace "root\virtualization" -Query "select * from Msvm_DiskDrive Where DriveNumber=2" #$Disk = Get-WmiObject -Namespace "root\virtualization" -Query "select * from Msvm_DiskDrive Where ElementName Like '%Lun 200%'"$DefaultHardDisk.Parent = $VmScsiController.__Path $DefaultHardDisk.Address = 0 $DefaultHardDisk.HostResource = $Disk.__PATH$VMManagementService.AddVirtualSystemResources($VM, $DefaultHardDisk.PSBase.GetText(1)) | ProcessWMIJob $VMManagementService "AddVirtualSystemResources" |
Taylor Brown
Hyper-V Integration Test Lead
https://blogs.msdn.com/taylorb
Comments
Anonymous
August 25, 2010
hi,sir,please help me,please!!! I know how to attaching a vhd ,but i don't know how to set the fix disk size,the defaultSystem size is 127G,so ,can you tell me how to do ,or give me some code! thanks!!!!!!!!!!Anonymous
October 10, 2010
Can you provide me the C# code to attach existing VHD , to a new VM at the time of its creation. regards S.RAnonymous
October 10, 2010
Can you provide me the C# code to attach existing VHD , to a new VM at the time of its creation. regards S.R