#Diskpart挂载VHD
$DriveLetter = "X"
$VHD_File = "e:\vhd\test\win2008r2.vhdx"
$CurrentPath = "e:\vhd"
Function Diskpart_Mount ($DriveLetter,$VHD_File)
{
$now = get-date -format 'yyyyMMddHHmm'
$dpmountcmd_ScriptName = "DPMount_script_" + $now + ".txt"
$dpmountcmd_ScriptPath = Join-Path -Path $CurrentPath -ChildPath $dpmountcmd_ScriptName
$dpmountcmd = @()
$dpmountcmd += ("Select Vdisk file" + " = """ + $VHD_File + """")
$dpmountcmd += ("Attach Vdisk")
$dpmountcmd += ("Select Partition 2")
$dpmountcmd += ("Assign letter=" + $DriveLetter)
$dpmountcmd += ("Exit")
Set-Content -Path $dpmountcmd_ScriptPath -Value $dpmountcmd
Diskpart -s $dpmountcmd_ScriptPath
}
Diskpart_Mount $DriveLetter $VHD_File
=================================================
#Diskpart卸载VHD
$DriveLetter = "X"
$VHD_File = "e:\vhd\test\win2008r2.vhdx"
$CurrentPath = "e:\vhd"
Function Diskpart_Unmount ($VHD_File)
{
$now = get-date -format 'yyyyMMddHHmm'
$dpunmountcmd_ScriptName = "DPUnmount_script_" + $now + ".txt"
$dpunmountcmd_ScriptPath = Join-Path -Path $CurrentPath -ChildPath $dpunmountcmd_ScriptName
$dpunmountcmd = @()
$dpunmountcmd += ("Select Vdisk file" + " = """ + $VHD_File + """")
$dpunmountcmd += ("Detach Vdisk")
$dpunmountcmd += ("Exit")
Set-Content -Path $dpunmountcmd_ScriptPath -Value $dpunmountcmd
Diskpart -s $dpunmountcmd_ScriptPath
}
Diskpart_Unmount $VHD_File