简体   繁体   English

如何使用 Windows 任务计划程序自动执行 PowerShell 脚本?

[英]How do I execute a PowerShell script automatically using Windows task scheduler?

I have one PowerShell script which sends emails.我有一个发送电子邮件的 PowerShell 脚本。 I want to execute that script automatically, every 1 minute.我想每 1 分钟自动执行一次该脚本。 How can I do it, using task scheduler?我该怎么做,使用任务调度程序?

Currently I have created a task and provided the path of my script.目前我已经创建了一个任务并提供了我的脚本的路径。 But that scheduler opens my script, instead of executing.但是该调度程序会打开我的脚本,而不是执行。

I am using Windows 7 Professional and PowerShell version 2.0.5.我使用的是 Windows 7 Professional 和 PowerShell 2.0.5 版。

Create the scheduled task and set the action to:创建计划任务并将操作设置为:

Program/Script: Powershell.exe程序/脚本: Powershell.exe

Arguments: -File "C:\\Users\\MyUser\\Documents\\ThisisMyFile.ps1"参数: -File "C:\\Users\\MyUser\\Documents\\ThisisMyFile.ps1"

Here is an example using PowerShell 3.0 or 4.0 for -RepeatIndefinitely and up:以下是使用 PowerShell 3.0 或 4.0 进行-RepeatIndefinitely及更高版本的-RepeatIndefinitely

# Trigger
$middayTrigger = New-JobTrigger -Daily -At "12:40 AM"
$midNightTrigger = New-JobTrigger -Daily -At "12:00 PM"
$atStartupeveryFiveMinutesTrigger = New-JobTrigger -once -At $(get-date) -RepetitionInterval $([timespan]::FromMinutes("1")) -RepeatIndefinitely

# Options
$option1 = New-ScheduledJobOption –StartIfIdle

$scriptPath1 = 'C:\Path and file name 1.PS1'
$scriptPath2 = "C:\Path and file name 2.PS1"

Register-ScheduledJob -Name ResetProdCache -FilePath $scriptPath1 -Trigger  $middayTrigger,$midNightTrigger -ScheduledJobOption $option1
Register-ScheduledJob -Name TestProdPing -FilePath $scriptPath2 -Trigger $atStartupeveryFiveMinutesTrigger

Instead of only using the path to your script in the task scheduler, you should start PowerShell with your script in the task scheduler, eg您应该使用任务计划程序中的脚本启动 PowerShell,而不是仅使用任务计划程序中脚本的路径,例如

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NonInteractive -File "C:\Path\To\Your\PS1File.ps1"

See powershell /?powershell /? for an explanation of those switches.对这些开关的解释。

If you still get problems you should read this question .如果你仍然遇到问题,你应该阅读这个问题

In my case, my script has parameters, so I set:就我而言,我的脚本有参数,所以我设置:

Arguments : -Command "& C:\\scripts\\myscript.ps1 myParam1 myParam2"参数-Command "& C:\\scripts\\myscript.ps1 myParam1 myParam2"

After several hours of test and research over the Internet, I've finally found how to start my PowerShell script with task scheduler, thanks to the video Scheduling a PowerShell Script using Windows Task Scheduler by Jack Fruh @sharepointjack .经过几个小时的互联网测试和研究,我终于找到了如何使用任务计划程序启动我的 PowerShell 脚本,这要归功于Jack Fruh @sharepointjack的视频使用 Windows 任务计划程序调度 PowerShell 脚本

Program/script -> put full path through powershell.exe程序/脚本 -> 通过 powershell.exe 放置完整路径

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe

Add arguments -> Full path to the script, and the script, without any " ".添加参数 -> 脚本和脚本的完整路径,不带任何“”。

Start in (optional) -> The directory where your script resides, without any " ".从(可选)开始 -> 脚本所在的目录,没有任何“”。

You can use the Unblock-File cmdlet to unblock the execution of this specific script.您可以使用 Unblock-File cmdlet 取消阻止此特定脚本的执行。 This prevents you doing any permanent policy changes which you may not want due to security concerns.这可以防止您进行任何出于安全考虑而可能不想要的永久性策略更改。

Unblock-File path_to_your_script

Source: Unblock-File来源: 解锁文件

None of posted solutions worked for me.没有一个已发布的解决方案对我有用。 Workaround, which worked:解决方法,有效:

create a run.bat and put inside powershell.exe -file "C:\\...\\script.ps1"创建一个run.bat并放入powershell.exe -file "C:\\...\\script.ps1"

then set Action to Program/Script: "C:\\...\\run.bat"然后将操作设置为程序/脚本: "C:\\...\\run.bat"

  1. Open the created task scheduler打开创建的任务调度程序

  2. switch to the “Action” tab and select your created “Action”切换到“操作”选项卡并选择您创建的“操作”

  3. In the Edit section, using the browser you could select powershell.exe in your system32\\WindowsPowerShell\\v1.0 folder.在编辑部分,您可以使用浏览器在 system32\\WindowsPowerShell\\v1.0 文件夹中选择 powershell.exe。

     Example -C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe
  4. Next, in the 'Add arguments' -File parameter, paste your script file path in your system.接下来,在“添加参数”-File 参数中,将您的脚本文件路径粘贴到您的系统中。

     Example – c:\\GetMFAStatus.ps1

This blog might help you to automate your Powershell scripts with windows task scheduler此博客可能会帮助您使用 Windows 任务计划程序自动执行 Powershell 脚本

That's perfect if you only want to execute PowerShell once, or on the scheduler for the task.如果您只想执行 PowerShell 一次,或者在任务的调度程序上执行,那将是完美的选择。 However, I want to send email notifications based on a schedule separate from Task Scheduler.但是,我想根据与任务计划程序不同的计划发送电子邮件通知。 For example, my task runs once per week.例如,我的任务每周运行一次。 I want to execute a PowerShell script every hour to verify the task is still running.我想每小时执行一次 PowerShell 脚本来验证任务是否仍在运行。 Any thoughts?有什么想法吗?

I also could not launch scripts, after heavy searching nothing helped.我也无法启动脚本,经过大量搜索后没有任何帮助。 No -ExecutionPolicy, no commands, no files and no difference between "" and ''.没有 -ExecutionPolicy,没有命令,没有文件,"" 和 '' 之间没有区别。

I simply put the command I ran in powershell in the argument tab: ./scripts.ps1 parameter1 11 parameter2 xx and so on.我只是将我在 powershell 中运行的命令放在参数选项卡中: ./scripts.ps1 parameter1 11 parameter2 xx等等。 Now the scheduler works.现在调度程序工作。 Program: Powershell.exe Start in: C:/location/of/script/程序: Powershell.exe开始于: C:/location/of/script/

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

相关问题 如何在Task Scheduler上执行PowerShell脚本? - How to execute PowerShell script on Task Scheduler? 如何使用 powershell 脚本禁用任务调度程序作业 - How can i disable task scheduler jobs using powershell script Windows Task Scheduler使用Powershell执行.aspx页 - Windows Task Scheduler to execute .aspx page using Powershell 如何使用 PowerShell 监视特定的任务调度程序事件 - How do I monitor a specific task scheduler event to using PowerShell 如何使用 PowerShell 脚本在 Windows 任务计划程序中配置“如果任务已在运行,则以下规则适用”? - How to configure "If the task is already running, then the following rule applies" in Windows Task Scheduler using PowerShell script? PowerShell 任务调度程序“CylancePROTECT”不执行脚本 - PowerShell script does not execute by task scheduler "CylancePROTECT" 如何在具有管理员权限的 Windows 任务计划程序中设置 Powershell 脚本? - How to setup a Powershell Script in Windows Task Scheduler with admin permissions? Windows Task Scheduler Powershell脚本完成并重新启动 - Windows Task Scheduler Powershell Script complete and restart 如何从Windows Powershell执行脚本? - How do I execute a script from Windows Powershell? 如何在静默/隐藏模式下使用任务调度程序运行powershell脚本? - How to run powershell script using task scheduler in silent/hidden mode?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM