How to dismount a VHDx file mounted in a folder?

Hello,

On a usb drive, I have a VHDx file (which is bitlocked).

I want to create two scripts to quickly mount and dismount the VHDx file on Windows 8 system.

I want to mount the drive in a subfolder on my usb disk.

I succeed in writing the mount script:

$scriptDir = Split-Path($MyInvocation.MyCommand.Definition)

$vhdPath = Join-Path $scriptDir "\_private\crypted.vhdx"
$mountPath = Join-Path $scriptDir "\Data"

if(-not (Get-ChildItem $mountPath -Force)) # check if not already mounted
{
    Mount-VHD -NoDriveLetter -Path $mountPath
}
else{
    Write-Warning "Already mounted"
}

However, the dismount script does not works:

$scriptDir = Split-Path($MyInvocation.MyCommand.Definition)

$vhdPath = Join-Path $scriptDir "\_private\crypted.vhdx"
$mountPath = Join-Path $scriptDir "\Data"

if(Get-ChildItem $mountPath) # check if not already mounted
{
    Dismount-VHD -Path $vhdPath
}
else{
    Write-Warning "Not mounted"
}

The Dismount-VHD -Path $vhdPath throw an error :

Dismount-VHD : lment N:_private\crypted.vhdx introuvable.

(can be translated to something like item N:\_private\crypted.vhdx cannot be found)

I also try with Dismount-VHD -Path $mountPath, but I get another exception:

Dismount-VHD : Le paramtre nest pas valide.  N:\Data  nest pas un fichier de disque dur virtuel existant.

(translation: Invalid parameter: n:\Data is not a disk file)

What is the correct way to dismount a VHD file?

Thanks,

August 6th, 2013 3:05am

Hello,

On a usb drive, I have a VHDx file (which is bitlocked).

I want to create two scripts to quickly mount and dismount the VHDx file on Windows 8 system.

I want to mount the drive in a subfolder on my usb disk.

I succeed in writing the mount script:

$scriptDir = Split-Path($MyInvocation.MyCommand.Definition)

$vhdPath = Join-Path $scriptDir "\_private\crypted.vhdx"
$mountPath = Join-Path $scriptDir "\Data"

if(-not (Get-ChildItem $mountPath -Force)) # check if not already mounted
{
    Mount-VHD -NoDriveLetter -Path $mountPath
}
else{
    Write-Warning "Already mounted"
}

However, the dismount script does not works:

$scriptDir = Split-Path($MyInvocation.MyCommand.Definition)

$vhdPath = Join-Path $scriptDir "\_private\crypted.vhdx"
$mountPath = Join-Path $scriptDir "\Data"

if(Get-ChildItem $mountPath) # check if not already mounted
{
    Dismount-VHD -Path $vhdPath
}
else{
    Write-Warning "Not mounted"
}

The Dismount-VHD -Path $vhdPath throw an error :

Dismount-VHD : lment N:_private\crypted.vhdx introuvable.

(can be translated to something like item N:\_private\crypted.vhdx cannot be found)

I also try with Dismount-VHD -Path $mountPath, but I get another exception:

Dismount-VHD : Le paramtre nest pas valide.  N:\Data  nest pas un fichier de disque dur virtuel existant.

(translation: Invalid parameter: n:\Data is not a disk file)

What is the correct way to dismount a VHD file?

Thanks,

Free Windows Admin Tool Kit Click here and download it now
August 6th, 2013 3:23am

the N drive is where the vhd mount or store ?

Thanks, I was able to update my script as expected:

$scriptDir = Split-Path($MyInvocation.MyCommand.Definition)

$vhdPath = Join-Path $scriptDir "\_private\crypted.vhdx"
$mountPath = Join-Path $scriptDir "\Data"

if(Get-ChildItem $mountPath -ea SilentlyContinue) # check if not already mounted
{
    $diskPartScriptContent = @( "select vdisk file=$vhdPath", "detach vdisk")
    $diskPartScriptFile = Join-Path $scriptDir "_diskpart.tmp"
    Set-Content  -Path $diskPartScriptFile -Value $diskPartScriptContent
    & diskpart.exe /s $diskPartScriptFile
    
}
else{
    Write-Warning "Not mounted"
}

My N: drive is my usb drive letter. I don't expect it to be fixed, that's why I put my scripts directly in the usb drive, and I calculate path dynamically.

I have still some issues with my mount script. Don't know why I thought it worked, but it actually does not works. I'll try to explore some diskpart syntax to solve this.

August 6th, 2013 3:58am

The final working scripts are these ones:

Mount.ps1 :

$scriptDir = Split-Path($MyInvocation.MyCommand.Definition)

$vhdPath = Join-Path $scriptDir "\_private\crypted.vhdx"
$mountPath = Join-Path $scriptDir "\Data"

if(-not (Get-ChildItem $mountPath -ea SilentlyContinue)) # check if not already mounted
{
    $diskPartScriptContent = @( 
        "select vdisk file=$vhdPath", 
        "attach vdisk"
        )
    $diskPartScriptFile = Join-Path $scriptDir "_diskpart.tmp"
    Set-Content  -Path $diskPartScriptFile -Value $diskPartScriptContent
    $result = (& diskpart.exe /s $diskPartScriptFile)
    
    & mountvol n:\data "\\?\Volume{820c8453-f504-11e2-bedd-9e2cd2a4b688}"

}
else{
    Write-Warning "Already mounted"
}

Dismount.ps1:

$scriptDir = Split-Path($MyInvocation.MyCommand.Definition)

$vhdPath = Join-Path $scriptDir "\_private\crypted.vhdx"
$mountPath = Join-Path $scriptDir "\Data"

if(Get-ChildItem $mountPath -ea SilentlyContinue) # check if not already mounted
{
    $diskPartScriptContent = @( 
        "select vdisk file=$vhdPath", 
        "detach vdisk"
        )
    $diskPartScriptFile = Join-Path $scriptDir "_diskpart.tmp"
    Set-Content  -Path $diskPartScriptFile -Value $diskPartScriptContent
    & diskpart.exe /s $diskPartScriptFile
    & mountvol $mountPath /d

}
else{
    Write-Warning "Not mounted"
}

I hardcoded the volume ID in my mount script. If someone knows a way to get this automatically, I'd appreciate. But I can live with that by now, because my volume is always the same.

Free Windows Admin Tool Kit Click here and download it now
August 6th, 2013 4:22am

Hi

you can use command mountvol C: /L

C is drive letter .

August 6th, 2013 4:31am

I don't have an assigned drive letter. 

Free Windows Admin Tool Kit Click here and download it now
August 6th, 2013 4:33am

I don't have an assigned drive letter. 

August 6th, 2013 4:38am

I guess I can fix it. Are you talking about the visible name in the windows explorer?

Free Windows Admin Tool Kit Click here and download it now
August 6th, 2013 4:39am

I guess I can fix it. Are you talking about the visible name in the windows exp

August 6th, 2013 4:47am

I guess I can fix it. Are you talking about the visible name in the windows exp

Free Windows Admin Tool Kit Click here and download it now
August 6th, 2013 4:49am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics