[英]How to schedule a powershell script to be executed right after booting on Windows?
I want to run a script starting some application on Windows when the machine is started/restarted. 启动/重新启动计算机时,我想在Windows上运行启动某些应用程序的脚本。
I tried the Register-ScheduledJob cmdlet : 我尝试了Register-ScheduledJob cmdlet:
Register-ScheduledJob -Name "MyJob" -ScriptBlock {.\C:\Users\Administrator\my_script.ps1 -input_value xyz} -Trigger (New-JobTrigger -AtStartup)
However, nothing gets executed at the startup and the output folder on 但是,启动时不会执行任何操作,并且输出文件夹位于
$home\AppData\Local\Microsoft\Windows\PowerShell\ScheduledJobs directory
The Task Scheduler tool does not show anything as well. 任务计划程序工具也不会显示任何内容。
my_script.ps1 simply starts a Java jar as follows: my_script.ps1只需按以下方式启动Java jar:
param(
[parameter(Mandatory=$TRUE,Position=0)]
[string] $input_value
)
write-output "Starting my Java app ... "
start powershell {java -jar slave.jar <<-some params ...>> ; Read-Host}
Exit 0
Even if I try to run the job with Start-Job -DefinitionName MyJob , nothing happens although it outputs that its running ... 即使我尝试使用Start-Job -DefinitionName MyJob运行作业,也不会发生任何反应,尽管它输出的是正在运行的内容...
Any ideas? 有任何想法吗?
The only way I know is if you run it in task scheduler . 我知道的唯一方法是是否在Task Scheduler中运行它。
schtasks /Create [/S <system> [/U <username> [/P [<password>]]]]
[/RU <username> [/RP <password>]] /SC <schedule> [/MO <modifier>] [/D <day>]
[/M <months>] [/I <idletime>] /TN <taskname> /TR <taskrun> [/ST <starttime>]
[/RI <interval>] [ {/ET <endtime> | /DU <duration>} [/K] [/XML <xmlfile>] [/V1]]
[/SD <startdate>] [/ED <enddate>] [/IT] [/Z] [/F]
I would create a Task
in Task Scheduler
and set the Trigger
as "When a specific event is logged" 我将在“
Task Scheduler
创建一个Task
,并将“ Trigger
”设置为“记录特定事件时”
Then, set Log:
to System and Source:
to Kernal-General and Event ID:
to 12 (or any event you would like to trigger the script on). 然后,将
Log:
设置为System , Source:
设置为Kernal-General , Event ID:
为12 (或者您希望触发脚本的任何事件)。 12 seems to always fire after a boot. 12似乎总是在启动后启动。
Under Action select Start a Program and set Program/script to PowerShell
. 在“操作”下,选择“ 启动程序”,然后将“ 程序/脚本”设置为
PowerShell
。 In Add arguments (optional): enter -file "C:\\YourDirectory\\YourScript.ps1"
. 在“ 添加参数”(可选)中:输入
-file "C:\\YourDirectory\\YourScript.ps1"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.