none
Relaunch command when error ( RRS feed

  • Question

  • Hi,

    I have this script which mounts a vhdx. It works fine but only thing is that when password for volume is not correctly entered, it doesn't ask for it again but continues to While .... (see script).
    I could catch the error with $error[0] or work with try catch. If I would use the while, while (!(Test-Path "T:\TestFolder")) { manage-bde -unlock T: -password } but this would create a loop.

    Please advise howto do catch this error then reask for the password

    This is the script
    Mount-DiskImage -ImagePath  c:\myvhdx.vhdx
    Set-disk 2 -IsOffline $False
    Set-Disk -Number 2 -IsReadonly $False
    manage-bde -unlock T: -password
    while (!(Test-Path "T:\TestFolder")) { Start-Sleep 1 }
    Start-Process -FilePath "C:\Dropbox\Dropbox.exe"

    ---

    When incorrect password:

    Enter the password to unlock this volume:
    ERROR: The password failed to unlock volume T:.




    Jan Hoedt

    Friday, February 12, 2016 1:09 PM

Answers

  • That's actually what I want: prompting for a password, which I manually enter. But it should ask it again if not correct. That's what I would like it do.

    Jan Hoedt

    Just put it in a loop.  Check the exitcode for success.

    if($LASTEXITCODE -eq 0){break}


    \_(ツ)_/

    • Marked as answer by janhoedt Friday, February 19, 2016 12:34 PM
    Thursday, February 18, 2016 5:58 PM
  • That also creates a loop of popups asking the password finally making the windows crash :-(

    Jan Hoedt

    It worked for me in the console. It does not work in the ISE.

    PS C:\> $trigger = $false
    PS C:\>
    PS C:\> do {
    >>
    >>     manage-bde -unlock d: -Password
    >>
    >>     If (Test-Path -Path D:) {
    >>
    >>         $trigger = $true
    >>
    >>     }
    >>
    >> } until ($trigger)
    >>
    BitLocker Drive Encryption: Configuration Tool version 6.1.7601
    Copyright (C) Microsoft Corporation. All rights reserved.
    
    Enter the password to unlock this volume:
    ERROR: The password failed to unlock volume D:.
    BitLocker Drive Encryption: Configuration Tool version 6.1.7601
    Copyright (C) Microsoft Corporation. All rights reserved.
    
    Enter the password to unlock this volume:
    The password successfully unlocked volume D:.
    PS C:\>


    • Marked as answer by janhoedt Friday, February 19, 2016 12:34 PM
    Friday, February 12, 2016 2:37 PM
  • Problem is that the manage-bde pops up a dosbox which makes thing more complicated (I get in a loop or it just closes).

    This works fine for me:


    Mount-DiskImage -ImagePath  C:\myvhdx.vhdx
    #here I'd need to be able to select the virtual disk which has volume name Data and is 50 GB only, so I can use the script on any pc => to check
    Set-disk 1 -IsOffline $False
    Set-Disk -Number 1 -IsReadonly $False
    
    while (!(Test-Path "T:\FolderName")) 
        {
            $SecurePassword = Read-Host -Prompt "Enter password" -AsSecureString
            Unlock-BitLocker -MountPoint "T:" -Password $securepassword
        }
    
    Start-Process -FilePath c:\... programs I need to start (which access files on the data vhdx like a Dropbox)



    Jan Hoedt

    • Marked as answer by janhoedt Friday, February 19, 2016 12:34 PM
    Friday, February 19, 2016 9:48 AM

All replies

  • if($LASTEXITCODE){'there was an error'}

    \_(ツ)_/

    Friday, February 12, 2016 1:36 PM
  • Try
    {
    manage-bde -unlock T: -password $password
    }
    Catch
    {
    write-host $LASTEXITCODE
    $password = read-host "Enter password again"
    manage-bde -unlock T: -password $password
    }
    Friday, February 12, 2016 1:47 PM
  • $trigger = $false
    
    do {
    
        manage-bde -Unlock T: -Password
    
        If (Test-Path -Path T:) {
    
            $trigger = $true
    
        }
    
    } until ($trigger)       


    • Proposed as answer by jrv Friday, February 12, 2016 3:03 PM
    • Unproposed as answer by jrv Friday, February 12, 2016 3:03 PM
    • Proposed as answer by jrv Thursday, February 18, 2016 6:15 PM
    Friday, February 12, 2016 2:09 PM
  • That also creates a loop of popups asking the password finally making the windows crash :-(

    Jan Hoedt

    Friday, February 12, 2016 2:23 PM
  • That also creates a loop of popups asking the password finally making the windows crash :-(

    Jan Hoedt

    It worked for me in the console. It does not work in the ISE.

    PS C:\> $trigger = $false
    PS C:\>
    PS C:\> do {
    >>
    >>     manage-bde -unlock d: -Password
    >>
    >>     If (Test-Path -Path D:) {
    >>
    >>         $trigger = $true
    >>
    >>     }
    >>
    >> } until ($trigger)
    >>
    BitLocker Drive Encryption: Configuration Tool version 6.1.7601
    Copyright (C) Microsoft Corporation. All rights reserved.
    
    Enter the password to unlock this volume:
    ERROR: The password failed to unlock volume D:.
    BitLocker Drive Encryption: Configuration Tool version 6.1.7601
    Copyright (C) Microsoft Corporation. All rights reserved.
    
    Enter the password to unlock this volume:
    The password successfully unlocked volume D:.
    PS C:\>


    • Marked as answer by janhoedt Friday, February 19, 2016 12:34 PM
    Friday, February 12, 2016 2:37 PM
  • Thanks, tried it in my scheduled task (since it needs to run with "highest" privilige). Maybe that's why it causes all the popups for me(?)

    Jan Hoedt

    Friday, February 12, 2016 2:45 PM
  • Try
    {
    manage-bde -unlock T: -password $password
    }
    Catch
    {
    write-host $LASTEXITCODE
    $password = read-host "Enter password again"
    manage-bde -unlock T: -password $password
    }

    This doesn't work.  Utilities do not throw exceptions back to callers.

    \_(ツ)_/

    Friday, February 12, 2016 3:03 PM
  • Thanks, tried it in my scheduled task (since it needs to run with "highest" privilige). Maybe that's why it causes all the popups for me(?)

    Jan Hoedt

    Yes, because you need to input the password manually.

        -Password or -pw
                    Prompt for a password to unlock the volume.


    Friday, February 12, 2016 3:43 PM
  • If I enter this, I get a dosprompt, but when I enter a wrong password, it exists.

    Jan Hoedt

    Thursday, February 18, 2016 4:50 PM
  • You cannot script this.  It is a security restriction.  You must enter the password manually.

    \_(ツ)_/

    Thursday, February 18, 2016 5:37 PM
  • That's actually what I want: prompting for a password, which I manually enter. But it should ask it again if not correct. That's what I would like it do.

    Jan Hoedt

    Thursday, February 18, 2016 5:51 PM
  • That's actually what I want: prompting for a password, which I manually enter. But it should ask it again if not correct. That's what I would like it do.

    Jan Hoedt

    Just put it in a loop.  Check the exitcode for success.

    if($LASTEXITCODE -eq 0){break}


    \_(ツ)_/

    • Marked as answer by janhoedt Friday, February 19, 2016 12:34 PM
    Thursday, February 18, 2016 5:58 PM
  • That's actually what I want: prompting for a password, which I manually enter. But it should ask it again if not correct. That's what I would like it do.

    Jan Hoedt

    What I posted originally does that (at least in my testing, see above for PoC).


    Thursday, February 18, 2016 6:14 PM
  • Yes - Mike's code will work although it I easier to use the exitcode to determine what went wrong.  YOu will get different exit code depending on the error type.  Bad password, drive not found, drive error, etc.

    \_(ツ)_/

    Thursday, February 18, 2016 6:16 PM
  • Problem is that the manage-bde pops up a dosbox which makes thing more complicated (I get in a loop or it just closes).

    This works fine for me:


    Mount-DiskImage -ImagePath  C:\myvhdx.vhdx
    #here I'd need to be able to select the virtual disk which has volume name Data and is 50 GB only, so I can use the script on any pc => to check
    Set-disk 1 -IsOffline $False
    Set-Disk -Number 1 -IsReadonly $False
    
    while (!(Test-Path "T:\FolderName")) 
        {
            $SecurePassword = Read-Host -Prompt "Enter password" -AsSecureString
            Unlock-BitLocker -MountPoint "T:" -Password $securepassword
        }
    
    Start-Process -FilePath c:\... programs I need to start (which access files on the data vhdx like a Dropbox)



    Jan Hoedt

    • Marked as answer by janhoedt Friday, February 19, 2016 12:34 PM
    Friday, February 19, 2016 9:48 AM
  • For selecting the virtual disk I could use

    Get-Disk -FriendlyName "Msft Virtual Disk"

    However, sometimes I'm using multiple virtual disks: one to boot from, one with data on it. The virtual disk don't have a serial number. I'd need to select on a different property.


    Jan Hoedt

    Friday, February 19, 2016 9:54 AM