简体   繁体   English

计划任务运行 Powershell 脚本

[英]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:我对此进行了研究并尝试了许多不同的论点:

  • Have attempted running with "Powershell" as the program, with the file path to the system folder storing powershell, or simply as PowerShell, or Powershell.exe.已尝试使用“Powershell”作为程序运行,使用存储 powershell 的系统文件夹的文件路径,或者简单地作为 PowerShell 或 Powershell.exe。
  • Have attempted the above (in every variation) with the -ExecutionPolicy Bypass switch.使用-ExecutionPolicy Bypass开关尝试了上述(在每个变体中)。
  • Have attempted the above with -File "C:\\Test\\V2\\Master.Script.ps1" and combined this with -ExecutionPolicy Bypass switch.已尝试使用-File "C:\\Test\\V2\\Master.Script.ps1" ,并将其与-ExecutionPolicy Bypass开关结合使用。
  • Attempted with "-NonInteractive" switch.尝试使用"-NonInteractive"开关。
  • Running as System作为系统运行
  • Running with Highest Priveleges以最高特权运行
  • Running whether user is logged on or not无论用户是否登录都运行
  • Tested using Domain User account which is Local Admin (On live environment, will have access to local admin or system only)使用本地管理员的域用户帐户进行测试(在实时环境中,只能访问本地管理员或系统)

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.

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