如果您有一个 .BAT 文件,并且您正尝试使用Windows中的任务计划程序(Task Scheduler)使其自动运行,您可能遇到了除非您手动运行任务,否则它根本无法运行的问题。
我创建了一个批处理文件,该文件在计算机启动时删除临时文件夹中的所有内容。我在任务计划程序(Task Scheduler)中创建了一个基本任务,并希望最好。不幸的是,当我的电脑启动时,什么也没发生。经过大量的试验和错误,我想出了如何让脚本运行。
在本文中,我将引导您完成需要调整的设置和权限,以便让您的批处理文件在没有人工干预的情况下运行。
第 1 步:检查File/Folder权限
解决此问题的第一步是确保您用于在任务计划程序(Task Scheduler)中运行脚本的帐户对包含脚本的文件夹、脚本本身以及脚本在运行时触及的任何文件夹/文件具有完全控制权限。(Full Control)
例如,我在下面创建了以下批处理脚本:
set folder="C:\test"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
我将.BAT文件保存到我的 Documents 文件夹中。路径是C:\Users\username\Documents。我去了 C:\Users\username,右键单击Documents文件夹,然后单击Properties。然后我点击了安全(Security)选项卡。
如您所见,用户帐户Aseem已被显式添加并授予完全控制(Full Control)权限。现在您必须对包含脚本的文件夹和脚本本身执行相同的操作。不要只是假设如果您授予包含脚本的文件夹的权限,您就可以开始了,因为您不是。最后,设置脚本将与之交互的任何文件和文件夹的权限。
就我而言,我必须转到C:\test,右键单击该文件夹并在其中添加具有完全控制(Full Control)权限的用户帐户。您必须这样做有点烦人,但这是让脚本运行的唯一方法。
注意:用于运行脚本的帐户必须是计算机上本地管理员组的一部分。就我而言,Aseem 帐户是管理员帐户,因此是本地管理员组的一部分。 (Note: The account that is being used to run the script has to be part of the local Administrators group on the computer. In my case, the Aseem account is an administrator account and therefore part of the local Administrators group. )
第 2 步:检查任务计划程序设置(Task Scheduler Settings)
现在让我们转到任务计划程序(Task Scheduler)并在那里更改适当的设置。打开任务计划程序并在(Task Scheduler)活动任务( Active Tasks)部分下找到您的任务。它们应按字母顺序列出。
双击它,它会在同一个窗口中自行打开任务。为了编辑任务,您必须右键单击它并选择Properties。
有几个选项卡,这里有几件事需要检查和更改。首先,在“常规(General)”选项卡上,您需要检查用于运行任务的用户帐户。在我的例子中,它是我之前在文件系统上授予权限的Aseem帐户,它是计算机上(Aseem)管理员(Administrators)组的一部分。
接下来,您必须选择无论用户是否登录都运行( Run whether user is logged on or not)选项,然后在配置(Configure for)框中选择Windows Vista、Windows Server 2008 。
在Actions选项卡上,您必须选择脚本,单击Edit ,然后在(Edit)Start in (optional)(e Start in (optional))框中添加包含脚本的文件夹的路径。这似乎没有必要,但事实并非如此。就我而言,我在框中输入了C:\Users\Aseem\Documents\。
现在单击确定(OK)以保存设置。执行此操作时,可能会出现一个对话框,您必须在其中输入将运行任务的用户帐户的密码。这就提出了另一个要求。您不能使用没有密码的帐户。用户帐户必须有密码才能运行任务。
最后,您应该在任务计划程序(Task Scheduler)中手动运行一次任务以确保它运行。如果在您更改所有设置后手动运行正常,那么它应该在应该触发时运行。就我而言,它应该在启动时发生,在我进行更改后,一切正常。
请注意,如果您的脚本在运行时正在访问域中的不同计算机,您应该尝试使用域管理员帐户来运行任务。这将确保该帐户具有足够的权限来访问远程计算机。
要注意的另一项是您的脚本是否访问网络共享上的资源。如果您的脚本使用字母访问网络,它可能无法运行。例如,您应该在脚本中使用\\machinename\share_name\data\F:\data\如果您仍然无法运行您的脚本,请在此处发表评论,我会尽力提供帮助。享受!
Fix Scheduled Task Won’t Run for .BAT File
If yоu have a .BAT file and you’re trying to get it to run automatically using Tаsk Scheduler in Windows, you might have run into the issue where it simply doesn’t run unless you manually run the task.
I created a batch file that deletes everything inside a temp folder whenever the computer starts up. I created a basic task in Task Scheduler and hoped for the best. Unfortunately, nothing happened when my computer booted up. After a lot of trial and error, I figured out how to get the script to run.
In this article, I’m going to walk you through the settings and permissions you need to adjust in order to get your batch file to run without manual intervention.
Step 1: Check File/Folder Permissions
The first step to fixing this issue is ensuring that the account you are using to run the script in Task Scheduler has Full Control permissions on the folder containing the script, the script itself, and any folders/files that the script touches when it runs.
For example, I created the following batch script below:
set folder="C:\test"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
I saved the .BAT file to my Documents folder. The path is C:\Users\username\Documents. I went to C:\Users\username, right-clicked on the Documents folder, and clicked on Properties. Then I clicked on the Security tab.
As you can see, the user account Aseem has been explicitly added and given the Full Control permission. Now you have to do the same thing for the folder that contains the script and for the script itself. Don’t just assume that if you give permissions to the folder containing the script, you’re good to go, because you’re not. Lastly, set permissions on any files and folders that the script will interact with.
In my case, I had to go to C:\test, right-click on that folder and add my user account there with Full Control permissions. It’s kind of annoying that you have to do this, but it’s the only way to get the script to run.
Note: The account that is being used to run the script has to be part of the local Administrators group on the computer. In my case, the Aseem account is an administrator account and therefore part of the local Administrators group.
Step 2: Check Task Scheduler Settings
Now let’s go to Task Scheduler and change the appropriate settings there. Open Task Scheduler and find your task under the Active Tasks section. They should be listed out in alphabetical order.
Double-click on it and it’ll open the task by itself in the same window. In order to edit the task, you’ll have to right-click on it and choose Properties.
There are several tabs and a couple of things have to checked and changed here. Firstly, on the General tab, you need to check the user account that is being used to run the task. In my case, it’s the Aseem account, which I had given permissions to earlier on the file system and which is part of the Administrators group on the computer.
Next, you have to choose the Run whether user is logged on or not option and choose Windows Vista, Windows Server 2008 in the Configure for box.
On the Actions tab, you have to select the script, click on Edit and then add in the path to the folder containing the script in the Start in (optional) box. This may seem unnecessary, but it’s not. In my case, I put in C:\Users\Aseem\Documents\ in the box.
Now click on OK to save the settings. When you do this, a dialog may appear where you have to enter the password for the user account that will run the task. This brings up another requirement. You can’t use an account that doesn’t have a password. The user account has to have a password in order for the task to run.
Lastly, you should run the task manually once in Task Scheduler to make sure it runs. If it runs fine manually after you changed all the settings, then it should run when it’s supposed to be triggered. In my case, it was supposed to happen on startup and after I made the changes, everything worked fine.
Note that if your script is accessing different computers in a domain when run, you should try to use the domain administrator account to run the task. This will ensure the account has enough permissions to access the remote computers.
Another item to note is if your script accesses resources on a network share. If your script is using letters to access the network, it may not run. For example, instead of using F:\data\, you should use \\machinename\share_name\data\ in the script. If you still can’t get your script to run, post a comment here and I’ll try to help. Enjoy!