Hyper-V How To: Add a PT Disk to A VM with a Script

Some friends here on the Hyper-V team shared a PowerShell 2.0 script for adding a pass-through disk to a VM:

# Add a pass-through disk to a VM

    [string]$vmName = $(throw "Must specify virtual machine name"),
    [int]$ideNum = $(throw "Must specify IDE location to attach the pass-through disk"),
    [int]$lunNum = $(throw "Must specify LUN location to attach the pass-through disk")

$vm = gwmi -namespace root\virtualization Msvm_ComputerSystem -filter "ElementName='$vmName'"

# Disks available for pass-through are associated to the physical disk resource pool
# (for this example, it is assumed there is only one disk available)
$diskPool = gwmi -namespace root\virtualization Msvm_ResourcePool `
    -filter "ResourceType = 22 AND ResourceSubtype = 'Microsoft Physical Disk Drive'"
$disk = gwmi -namespace root\virtualization `
    -query "Associators of {$diskPool} where ResultClass=Msvm_DiskDrive"

# Get the virtual machine's setting data
$vssd = gwmi -namespace root\virtualization `
    -query "Associators of {$vm} where ResultClass=Msvm_VirtualSystemSettingData" |`
    where{$_.SettingType -eq 3}

# Get IDE Controller
$ide = gwmi -namespace root\virtualization `
    -query "Associators of {$vssd} where ResultClass=Msvm_ResourceAllocationSettingData" |`
    where{$_.ResourceType -eq 5 -and $_.Address -eq $ideNum}

# Create a new Hard Disk Resource Allocation object for a disk at specified lun,
# and connect it to the server disk
$allocationCapabilities = gwmi -namespace root\virtualization Msvm_AllocationCapabilities `
    -filter "ResourceType = 22 AND ResourceSubtype = 'Microsoft Physical Disk Drive'"
$allocationPath = $allocationCapabilities.__PATH.replace("\","\\")
$default = gwmi -namespace root\virtualization Msvm_SettingsDefineCapabilities `
    -filter "ValueRange = 0 AND GroupComponent = '$allocationPath'"

$hardDisk = [wmi]$default.PartComponent
$hardDisk.Address = $lunNum
$hardDisk.HostResource = @($disk.__PATH)
$hardDisk.Parent = $ide.__PATH

# Attach the new Hard Disk to the virtual machine
$vmms = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService

$result = $vmms.AddVirtualSystemResources($vm,@($hardDisk.GetText(1)))

if($result.ReturnValue -eq 4096){
    # A Job was started, and can be tracked using its Msvm_Job instance
    $job = [wmi]$result.Job
    # Wait for job to finish
    while($job.jobstate -lt 7){$job.get()}
    # Return the Job's error code
    return $job.ErrorCode
# Otherwise, the method completed
return $result.ReturnValue

Useful? Want to see more sample scripts? Leave feedback.

For more info on how to use PS cmdlets see: http://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/index.mspx 

See also James O’Neil’s New and improved PowerShell Library for Hyper-V. Now with more functions and... documentation!

For all 35 sample Hyper-V PS1 scripts in a zipfile, go to: Hyper-V PowerShell Example Scripts.zip-download

Comments (0)

Skip to main content