在任务计划程序中运行 PowerShell [英] Running PowerShell in Task Scheduler

查看:64
本文介绍了在任务计划程序中运行 PowerShell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PowerShell 从电子邮件下载数据.

I am using PowerShell for downloading data from email.

我想通过 PowerShell 运行此进程.当我像这样运行脚本时:

I want to run this process by PowerShell. When I run script like this:

D:\script.ps1

powershell.exe 中它工作正常.

当我在任务计划程序中安排它时,没有任何反应.

When I schedule it in Task Scheduler nothing happens.

我尝试将其设置为程序/脚本:

I tried it to Set it like Program/script:

powershell
Powershell.exe
powershell.exe

添加参数:

-executionpolicy bypass -file D:\script.ps1
-file D:\script.ps1
-file "D:\script.ps1"

没有任何效果.我使用的是 Windows 2008 R2.

And nothing works. I'm using Windows 2008 R2.

推荐答案

对计划任务进行故障排除是一件很头疼的事,因为您无法真正了解正在发生的事情.以下是您可能需要检查的一些事项:

Troubleshooting scheduled tasks is a pain in the rear, because you can't really see what's going on. These are some things you may want to check:

  • 检查您的命令行是否正常工作,例如通过从 CMD 运行它(在你的情况下尝试运行 powershell.exe -File "D:\script.ps1").如果这给您带来任何错误,您需要先修复这些错误.

  • Check that your commandline works in principle, e.g. by running it from CMD (in your case try running powershell.exe -File "D:\script.ps1"). If that gives you any errors you need to fix those first.

如果您打算以特定用户身份运行任务,请以该用户身份启动 CMD 并运行相同的命令行以检查该用户是否具有执行脚本所需的所有权限.

If you intend to run the task as a particular user, start CMD as that user and run the same commandline to check if the user has all the permissions required for whatever the script is doing.

检查您的任务是否实际终止或进程是否仍在运行(通过 Process ExplorerGet-Process、任务管理器、…).

Check if your task actually terminated or if the process is still running (via Process Explorer, Get-Process, Task Manager, …).

检查上次运行结果以获取命令的退出代码.

Check the Last Run Result for the exit code of the command.

启用计划任务的历史记录(操作 → 启用所有任务历史记录).这至少会给你一些关于任务正在做什么的信息,它是否开始,以及是否/发生了哪些错误.您需要管理权限才能启用任务历史记录.

Enable the history for your scheduled tasks (Action → Enable All Tasks History). That will give you at least some information about what the task is doing, whether it starts at all, and if/which errors occurred. You need administrative rights to enable the task history.

检查事件日志中是否有与任务运行相关的错误/警告.

Check the eventlog for errors/warnings correlating with the task run.

将日志语句添加到您正在运行的脚本中以记录进度信息.我个人更喜欢记录到事件日志,因为这样可以避免文件系统权限问题.

Add logging statements to the script you're running to record progress information. Personally I prefer logging to the eventlog, because that avoids filesystem permissions issues.

Write-EventLog -LogName Application -Source EventSystem -EventID 100 -EntryType Information -Message 'Your log message.'

如果您在系统上拥有管理员权限,您可以注册自己的事件源并在上面的日志语句中使用它,而不是滥用像 EventSystem 这样的现有源:

If you have admin privileges on the system you can register an event source of your own and use that in the above log statement instead of abusing an existing source like EventSystem:

New-EventLog -Source MyEventSource -LogName Application

进一步的帮助将在很大程度上取决于您按照这些步骤获得的结果以及您的实际脚本代码.

Further help will depend heavily on the findings you got following these steps as well as your actual script code.

这篇关于在任务计划程序中运行 PowerShell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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