Using Diskpart in a PowerShell script won't allow script to reuse drive letter

Posted by Kyle on Server Fault See other posts from Server Fault or by Kyle
Published on 2012-12-11T00:19:05Z Indexed on 2012/12/11 5:06 UTC
Read the original article Hit count: 612

Filed under:
|
|
|
|

I built a script that mounts (attach) a VHD using Diskpart, cleans out some system files and then unmounts (detach) it. It uses a foreach loop and is suppose to clean multiple VHD using the same drive letter. However, after the 1st VHD it fails. I also noticed that when I try to manually attach a VHD with diskpart, diskpart succeeds, the Disk Manager shows the disk with the correct drive letter, but within the same PoSH instance I can not connect (set-location) to that drive. If I do a manual diskpart when I 1st open PoSH I can attach and detach all I want and I get the drive letter every time. Is there something I need to do to reset diskpart in the script? Here's a snippet of the script I'm using.

function Mount-VHD {
         [CmdletBinding()]
         param (
                 [Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$false)]
                         [string]$Path,
                 [Parameter(Position=1,Mandatory=$false,ValueFromPipeline=$false)]
                         [string]$DL,
                 [string]$DiskpartScript = "$env:SystemDrive\DiskpartScript.txt",
                 [switch]$Rescan
         )

         begin {
                 function InvokeDiskpart {
                         Diskpart.exe /s $DiskpartScript
                 }
                 ## Validate Operating System Version ##
                 if (Get-WmiObject win32_OperatingSystem -Filter "Version < '6.1'") {throw "The script operation requires at least Windows 7 or Windows Server 2008 R2."}
         }
         process{
                 ## Diskpart Script Content ## Here-String statement purposefully not indented ##
 @"
 $(if ($Rescan) {'Rescan'})
 Select VDisk File="$Path" `nAttach VDisk
 Exit
 "@ | Out-File -FilePath $DiskpartScript -Encoding ASCII -Force
                 InvokeDiskpart
 Start-Sleep -Seconds 3
 @"
 Select VDisk File="$Path"`nSelect partition 1 `nAssign Letter="$DL"
 Exit
 "@ | Out-File -FilePath $DiskpartScript -Encoding ASCII -Force
                 InvokeDiskpart
                         }
         end {
                 Remove-Item -Path $DiskpartScript -Force ; ""
                 Write-Host "The VHD ""$Path"" has been successfully mounted." ; ""
         }
 }
 function Dismount-VHD {
         [CmdletBinding()]
         param (
                 [Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$false)]
                         [string]$Path,
                 [switch]$Remove,
                 [switch]$NoConfirm,
                 [string]$DiskpartScript = "$env:SystemDrive\DiskpartScript.txt",
                 [switch]$Rescan
         )

         begin {
                 function InvokeDiskpart {
                         Diskpart.exe /s $DiskpartScript
                 }

                 function RemoveVHD {
                         switch ($NoConfirm) {
                                 $false {
                                         ## Prompt for confirmation to delete the VHD file ##
                                         "" ; Write-Warning "Are you sure you want to delete the file ""$Path""?"
                                         $Prompt = Read-Host "Type ""YES"" to continue or anything else to break"
                                         if ($Prompt -ceq 'YES') {
                                                 Remove-Item -Path $Path -Force
                                                 "" ; Write-Host "VHD ""$Path"" deleted!" ; ""
                                         } else {
                                                 "" ; Write-Host "Script terminated without deleting the VHD file." ; ""
                                         }
                                 }
                                 $true {
                                         ## Confirmation prompt suppressed ##
                                         Remove-Item -Path $Path -Force
                                         "" ; Write-Host "VHD ""$Path"" deleted!" ; ""
                                 }
                         }
                 }
                 ## Validate Operating System Version ##
                 if (Get-WmiObject win32_OperatingSystem -Filter "Version < '6.1'") {throw "The script operation requires at least Windows 7 or Windows Server 2008 R2."}
         }
         process{
         ## DiskPart Script Content ## Here-String statement purposefully not indented ##
 @"
 $(if ($Rescan) {'Rescan'})
 Select VDisk File="$Path"`nDetach VDisk
 Exit
 "@ | Out-File -FilePath $DiskpartScript -Encoding ASCII -Force
                 InvokeDiskpart
                 Start-Sleep -Seconds 10
         }
         end {
                 if ($Remove) {RemoveVHD}
                 Remove-Item -Path $DiskpartScript -Force ; ""
         }
 }

© Server Fault or respective owner

Related posts about Windows

Related posts about scripting