Search code examples
windowsbatch-filetruecrypt

Read variable from external file not working on running as scheduled task


I would like to run a batch file after resuming from sleep state in Windows.

If I start the batch file on command line everything works as expected.

But the batch script does not run properly as scheduled task.

What I have done:

External config file AutoMountConf.bat contains set Pass = Test

Local script file scheduleTask.bat contains

rem AutoMountConf.bat is in my intranet.
call  X:\AutoMountConf.bat
start "" "C:\Program Files\TrueCrypt\TrueCrypt.exe" /auto favorites /p %Pass% /q

On command line the TrueCrypt container is mounted. If I run the script from scheduled task I get the login screen to type the password manually.


Solution

  • There are two or perhaps even three issues.

    The first one is set Pass = Test instead of set "Pass=Test" as Stephan reported already. For more details on how to assign a value right to an environment variable see my answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line?


    The second issue is caused by the fact that network drives once mapped by a user to a drive letter and remembered in registry by Windows are automatically disconnected by Windows on user logs off and are only reconnected if the same user logs on again.

    For a scheduled task it is therefore very often necessary to use UNC paths for files and folders on a shared folder in network or connect the network drive and disconnect it in the batch file itself executed as scheduled task.

    It is not possible to call a batch file with UNC path. Windows does not allow that. Therefore it is necessary to connect and disconnect to network share manually in the batch file. I offer 2 solutions for this problem.

    The first one is using command net use:

    %SystemRoot%\System32\net.exe use X: \\ComputerName\ShareName password /user:Domain\UserName /persistent:no
    if not errorlevel 1 (
        call X:\AutoMountConf.bat
        %SystemRoot%\System32\net.exe use X: /delete
        start "" /wait "C:\Program Files\TrueCrypt\TrueCrypt.exe" /auto favorites /p %Pass% /q
    )
    

    password and /user:Domain\UserName is necessary only if the scheduled task is not executed with a user account which has the permissions to access the batch file on the remote machine. In general it is much more secure to define the scheduled task with the right user account and safe the password also for this account together with the task. Windows stores the password for the task encrypted like it does it also for the user account itself.

    Run in a command prompt windows net use /? for details on the required and optional options. /persistent:no is what avoids remembering the network share in Windows registry for automatic reconnect after login by same user.

    The second one is using commands pushd and popd:

    pushd \\ComputerName\ShareName
    if not errorlevel 1 (
        call AutoMountConf.bat
        popd
        start "" /wait "C:\Program Files\TrueCrypt\TrueCrypt.exe" /auto favorites /p %Pass% /q
    )
    

    Please execute in a command prompt window pushd /? and read the output help to understand why this works.

    But this solution requires that the user account used for the scheduled task with correct password is one which has appropriate permissions on the share on the remote computer. Password and user name can't be specified with this solution in the batch file itself.

    if not errorlevel 1 means if previous command exited NOT with a value greater or equal 1 meaning if exit code of previous command is 0 and therefore command execution was successful. It can always happen that the remote machine is currently not available on network and therefore it is always good to check success on connecting to share on remote machine.


    There is perhaps one more reason why Pass is not defined after running AutoMountConf.bat.

    AutoMountConf.bat contains setlocal and the variable Pass is defined after this command was executed and before endlocal is executed in same batch file or implicitly called by command processor on exiting AutoMountConf.bat.

    setlocal results in creating always a copy of existing environment variables and all modifications on environment variables are done on this local copy. The previous environment variables are restored on execution of (matching) endlocal or when end of a batch file is reached in which case the command processor automatically restores previous environment.

    Please execute in a command prompt window setlocal /? and read the output help.

    For examples to understand environment management by commands setlocal and endlocal perhaps even better see answers on Echoing a URL in Batch and Why is my cd %myVar% being ignored?