简体   繁体   English

如何让Powerershell在计划任务中执行?

[英]How do I get Powershell to execute in scheduled task?

I have created a Powershell script and set it up to run within a .bat file. 我创建了一个Powershell脚本并将其设置为在.bat文件中运行。 When I click on the .bat file the Powershell script runs like a champ. 当我点击.bat文件时,Powershell脚本像冠军一样运行。 I then set up the .bat file to be run as a scheduled task. 然后,我将.bat文件设置为作为计划任务运行。 Again it works like a champ when I right click on it and launch it interactively. 当我右键单击并以交互方式启动它时,它再次像冠军一样工作。 Problem is it fails when the task scheduled to run unattended in the evening. 问题是当任务计划在晚上无人值守时运行失败。

Not sure if this matters but I am using Powershell v.1 on Windows 2003 Server 64. 不确定这是否重要,但我在Windows 2003 Server 64上使用Powershell v.1。

Thanks in advance for your help. 在此先感谢您的帮助。

========================================== The status on the scheduled task is "Could not start". ==========================================计划任务的状态是“无法启动“。 Perhaps fails was the wrong choice of words because the task probably doesn't even start running. 也许失败是错误的选择,因为任务可能甚至没有开始运行。

The task is set up to run as my user and I am set up as a local administrator on the box. 该任务设置为以我的用户身份运行,并且我在该框中设置为本地管理员。

Check if scheduled task's security setting Scheduled task might be running under a credential that does not have access to the batch file. 检查计划任务的安全设置计划任务是否可能在无权访问批处理文件的凭据下运行。

When you are adding a new task, you are asked to enter a credential 添加新任务时,系统会要求您输入凭据

替代文字

And you can check for already created task's credential 您可以检查已创建的任务的凭证 替代文字

[UPDATE] [UPDATE]

The status on the scheduled task is "Could not start". 计划任务的状态为“无法启动”。 Perhaps fails was the wrong choice of words because the task probably doesn't even start running. 也许失败是错误的选择,因为任务可能甚至没有开始运行。

Make sure that Task Scheduler service is running 确保Task Scheduler服务正在运行
替代文字

Is the task set to run with your useraccount or as System? 任务是否设置为使用useraccount或System? Check the access rights on cmd.exe and powershell.exe. 检查cmd.exe和powershell.exe上的访问权限。

Also if you go to the sheduled tasks you can do a 'view log' in the advanced menu to check for error messages. 此外,如果您转到已完成的任务,您可以在高级菜单中执行“查看日志”以检查错误消息。

Just a guess, but could it be something to do with the account/security context from which the script is running? 只是一个猜测,但它可能与脚本运行的帐户/安全上下文有关吗? If it's running on the System account, then that could certainly be a problem. 如果它在系统帐户上运行,那么这肯定是个问题。 (I suspect it's just running on your user account however.) Check that all your path variables are set correctly when the script runs, for a start. (我怀疑它只是在你的用户帐户上运行。)检查脚本运行时是否正确设置了所有路径变量,以便开始。 Otherwise, could you perhaps provide any more information on how exactly it fails (ie any error messages)? 否则,您是否可以提供有关其失败的更多信息(即任何错误消息)?

Maybe the current directory is not set as you expect when running from the task manager. 从任务管理器运行时,可能没有按预期设置当前目录。 Try running your bat file from the command line, while you are not in the same directory as the file (and your .ps1 script) are. 尝试从命令行运行bat文件,同时与文件(和.ps1脚本)不在同一目录中。

Otherwise: check permissions. 否则:检查权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM