从外部文件读取变量无法按计划任务运行 [英] Read variable from external file not working on running as scheduled task

查看:169
本文介绍了从外部文件读取变量无法按计划任务运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows中从睡眠状态恢复后,我想运行一个批处理文件.

如果我在命令行上启动批处理文件,一切都会按预期进行.

但是批处理脚本不能作为计划任务正常运行.

我做了什么:

外部配置文件AutoMountConf.bat包含set Pass = Test

本地脚本文件scheduleTask.bat包含

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

在命令行上安装了TrueCrypt容器. 如果我从计划任务中运行脚本,则会出现登录屏幕以手动输入密码.

解决方案

有两个甚至三个问题.

第一个是set Pass = Test而不是set "Pass=Test",因为 Stephan 已有报道.有关如何为环境变量分配值权限的更多详细信息,请参见我在为什么没有使用'echo%var%'输出的字符串的答案.在命令行上使用"set var = text"之后?


第二个问题是由以下事实引起的:用户一旦将网络驱动器映射到驱动器号并由Windows记住在注册表中,网络驱动器将在用户注销时由Windows自动断开连接,并且仅在同一用户再次登录时才重新连接.

因此,对于计划任务,通常需要对网络共享文件夹中的文件和文件夹使用UNC路径,或者连接网络驱动器并断开作为计划任务执行的批处理文件本身的连接.

无法使用UNC路径调用批处理文件. Windows不允许这样做.因此,有必要在批处理文件中手动连接和断开与网络共享的连接.针对此问题,我提供了两种解决方案.

第一个使用命令 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/user:Domain\UserName.通常,使用正确的用户帐户定义计划的任务要安全得多,同时还要将该帐户的密码与任务一起安全保存. Windows将加密任务的密码存储起来,就像用户帐户本身一样.

在命令提示符窗口net use /?中运行,以获取有关必需和可选选项的详细信息. /persistent:no是避免在Windows注册表中记住同一用户登录后自动重新连接的网络共享的方法.

第二个使用命令 pushd 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
)

请在命令提示符窗口pushd /?中执行并阅读输出帮助以了解其工作原理.

但是,此解决方案要求用于计划任务的用户帐户具有正确的密码,该帐户必须对远程计算机上的共享具有适当的权限.此解决方案不能在批处理文件本身中指定密码和用户名.

if not errorlevel 1表示前一个命令是否以大于或等于1的值退出NOT,这意味着前一个命令的退出代码为0,因此命令执行成功.总是会发生远程计算机当前在网络上不可用的情况,因此,最好检查连接远程计算机上的共享是否成功.


运行AutoMountConf.bat后为什么未定义Pass可能还有一个原因.

AutoMountConf.bat包含 setlocal ,并且变量Pass是在该命令执行之后,在同一批处理文件中执行 endlocal 或由命令处理器隐式调用之前定义的在退出AutoMountConf.bat时.

setlocal 导致始终创建现有环境变量的副本,并且对环境变量的所有修改都在此本地副本上完成.在执行(匹配) endlocal 或到达批处理文件的末尾时,将还原先前的环境变量,在这种情况下,命令处理器将自动还原先前的环境.

请在命令提示符窗口setlocal /?中执行并阅读输出帮助.

有关通过命令 setlocal endlocal 了解环境管理的示例,也许甚至更好,请参见为什么我的CD%myVar%被忽略了?

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.

解决方案

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?

这篇关于从外部文件读取变量无法按计划任务运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆