[英]Scheduled Task Running Powershell Script
I am attempting to create a scheduled task via powershell, to run another powershell script which will call a number of other scripts on a daily basis.我正在尝试通过 powershell 创建计划任务,以运行另一个 powershell 脚本,该脚本将每天调用许多其他脚本。
When running any of these scripts from Powershell directly/manually, there are no issues - each script performs it's function with no issues whatsoever.当直接/手动从 Powershell 运行这些脚本中的任何一个时,没有问题 - 每个脚本执行它的功能都没有任何问题。
However, despite this, when attempting to run the task, I am hitting a brick wall with exit code 0x1
.但是,尽管如此,在尝试运行任务时,我还是碰到了退出代码为
0x1
的砖墙。
I have researched this and tried a number of various arguments:我对此进行了研究并尝试了许多不同的论点:
-ExecutionPolicy Bypass
switch.-ExecutionPolicy Bypass
开关尝试了上述(在每个变体中)。-File "C:\\Test\\V2\\Master.Script.ps1"
and combined this with -ExecutionPolicy Bypass
switch.-File "C:\\Test\\V2\\Master.Script.ps1"
,并将其与-ExecutionPolicy Bypass
开关结合使用。"-NonInteractive"
switch."-NonInteractive"
开关。I have had issues with this in the past as well.我过去也遇到过这个问题。 What I have found works is by using a
BAT File
to call the ps1 file.我发现有效的是使用
BAT File
来调用 ps1 文件。 Hopefully this help with your scenario.希望这对您的场景有所帮助。
BAT File will change execution policy and then run the ps1 file. BAT 文件将更改执行策略,然后运行 ps1 文件。 BAT and ps1 file need to be named the same except the file extension
BAT 和 ps1 文件需要命名相同,但文件扩展名除外
REG ADD "HKLM\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell" /T REG_SZ /V ExecutionPolicy /D Unrestricted /F
Start PowerShell.exe -Command "& '%~dpn0.ps1'"
Here is some code I have used to make a new task to run additional ps1 file.这是我用来创建新任务以运行其他 ps1 文件的一些代码。
$Task_Action = New-ScheduledTaskAction -Execute 'PowerShell.exe' -Argument '-File C:\File.ps1'
$Task_Principal = New-ScheduledTaskPrincipal -UserId SYSTEM -RunLevel Highest
$Task_Settings = New-ScheduledTaskSettingsSet -Hidden
$Task_Trigger = New-ScheduledTaskTrigger -AtStartup
Register-ScheduledTask `
-TaskName "Your Task Name" `
-Action $Task_Action `
-Principal $Task_Principal `
-Trigger $Task_Trigger `
-Settings $Task_Settings `
-Force
On the last script I set the execution policy back with Set-ExecutionPolicy Restricted -Scope LocalMachine
在最后一个脚本中,我使用
Set-ExecutionPolicy Restricted -Scope LocalMachine
重新设置了执行策略
I solved this issue using the answer from @briantist in PowerShell script won't execute as a Windows scheduled task , but I wanted to isolate exactly which switch was solving the problem.我在PowerShell 脚本中使用@briantist 的答案解决了这个问题, 不会作为 Windows 计划任务执行,但我想确切地隔离哪个开关正在解决问题。
It had nothing to do with -ExecutionPolicy
, -Noninteractive
, -NoLogo
, -NoProfile
or any other system privilege, user account running the script, etc.它与
-ExecutionPolicy
、 -Noninteractive
、 -NoLogo
、 -NoProfile
或任何其他系统权限、运行脚本的用户帐户等-NoProfile
。
Just needed to add -File
in front of the script path in the Task Scheduler > Actions > Arguments
field.只需要在
Task Scheduler > Actions > Arguments
字段中的脚本路径前面添加-File
。 Without this switch PowerShell was launching and the task history was showing Action Completed
, but the script was not executing.没有这个开关,PowerShell 正在启动,任务历史记录显示
Action Completed
,但脚本没有执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.