使用powershell计划运行可执行文件

使用powershell计划运行可执行文件,powershell,scheduled-tasks,Powershell,Scheduled Tasks,我需要写一个脚本,这将创建任务调度程序的工作,在电脑启动将运行可执行的。简言之,脚本将如下所示: $trigger = New-JobTrigger -AtStartup -RandomDelay 00:00:10 Register-ScheduledJob -Trigger $trigger -Name FileJob -ScriptBlock {$args[0]} 但它不起作用。怎么了 我使用以下Powershell代码段创建Windows计划任务 $action = New-Schedu

我需要写一个脚本,这将创建任务调度程序的工作,在电脑启动将运行可执行的。简言之,脚本将如下所示:

$trigger = New-JobTrigger -AtStartup -RandomDelay 00:00:10
Register-ScheduledJob -Trigger $trigger -Name FileJob -ScriptBlock {$args[0]}

但它不起作用。怎么了

我使用以下Powershell代码段创建Windows计划任务

$action = New-ScheduledTaskAction -Execute 'application.exe' -Argument '-NoProfile -WindowStyle Hidden'
$trigger =  New-ScheduledTaskTrigger -AtStartup -RandomDelay 00:00:10
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "FileJob" -Description "FileJob"

我使用以下Powershell代码段创建Windows计划任务

$action = New-ScheduledTaskAction -Execute 'application.exe' -Argument '-NoProfile -WindowStyle Hidden'
$trigger =  New-ScheduledTaskTrigger -AtStartup -RandomDelay 00:00:10
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "FileJob" -Description "FileJob"

这是我用来创建任务的工具:

#The first command uses the New-ScheduledTaskAction cmdlet to assign the action variable $A to the executable file tskmgr.exe
$A = New-ScheduledTaskAction -Execute "C:\WINDOWS\SysWOW64\WindowsPowerShell\v1.0\powershell.exe" -file "C:\directory\powershell.ps1"

#The second command uses the New-ScheduledTaskTrigger cmdlet to assign the trigger variable $T to the value AtLogon
$T = New-ScheduledTaskTrigger -weekly -DaysOfWeek Saturday -At 7am

#The third command uses the New-ScheduledTaskSettingsSet cmdlet to assign the settings variable $S to a task settings object
$S = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -DontStopOnIdleEnd -ExecutionTimeLimit 1:00:00 -MultipleInstances 2

#The fourth command assigns the principal variable to the New-ScheduledTaskPrincipal of the scheduled task, Domain\Username
$P = New-ScheduledTaskPrincipal -UserId domain\gMSAaccount$ -LogonType Password -RunLevel Highest

#The fifth command sets the description varible to $D for the task definition
$D = "Insert your description here"

#Registers the new scheduled task and defines it by using the $D variable
Register-ScheduledTask Name_of_Task -Action $A -Trigger $T -Settings $S -Principal $P -Description $D<p>
#第一个命令使用New ScheduledTaskAction cmdlet将操作变量$A分配给可执行文件tskmgr.exe
$A=新建ScheduledTaskAction-执行“C:\WINDOWS\SysWOW64\WindowsPowerShell\v1.0\powershell.exe”-文件“C:\directory\powershell.ps1”
#第二个命令使用New-ScheduledTaskTrigger cmdlet将触发器变量$T分配给值AtLogon
$T=新的计划任务装配工-每周-每周星期六-上午7点
#第三个命令使用New-ScheduledTaskSettingsSet cmdlet将设置变量$S分配给任务设置对象
$S=新的ScheduledTaskSettingSet-AllowsArtifonBatteries-DontStopIfGoingOnBatteries-DontStopOnNidleEnd-ExecutionTimeLimit 1:00:00-MultipleInstances 2
#第四个命令将主变量分配给计划任务的新ScheduledTaskPrincipal,Domain\Username
$P=New ScheduledTaskPrincipal-UserId domain\gMSAaccount$-LogonType Password-RunLevel Highest
#第五个命令将任务定义的description变量设置为$D
$D=“在此处插入您的描述”
#注册新的计划任务并使用$D变量定义它
注册任务的ScheduledTask名称\u任务-操作$A-触发器$T-设置$S-主体$P-描述$D


我发现在active directory环境中使用gMSA帐户是最好的
要求是,为了使用gMSA,必须使用powershell脚本或schtask.exe创建任务。

这是我用来创建任务的工具:

#The first command uses the New-ScheduledTaskAction cmdlet to assign the action variable $A to the executable file tskmgr.exe
$A = New-ScheduledTaskAction -Execute "C:\WINDOWS\SysWOW64\WindowsPowerShell\v1.0\powershell.exe" -file "C:\directory\powershell.ps1"

#The second command uses the New-ScheduledTaskTrigger cmdlet to assign the trigger variable $T to the value AtLogon
$T = New-ScheduledTaskTrigger -weekly -DaysOfWeek Saturday -At 7am

#The third command uses the New-ScheduledTaskSettingsSet cmdlet to assign the settings variable $S to a task settings object
$S = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -DontStopOnIdleEnd -ExecutionTimeLimit 1:00:00 -MultipleInstances 2

#The fourth command assigns the principal variable to the New-ScheduledTaskPrincipal of the scheduled task, Domain\Username
$P = New-ScheduledTaskPrincipal -UserId domain\gMSAaccount$ -LogonType Password -RunLevel Highest

#The fifth command sets the description varible to $D for the task definition
$D = "Insert your description here"

#Registers the new scheduled task and defines it by using the $D variable
Register-ScheduledTask Name_of_Task -Action $A -Trigger $T -Settings $S -Principal $P -Description $D<p>
#第一个命令使用New ScheduledTaskAction cmdlet将操作变量$A分配给可执行文件tskmgr.exe
$A=新建ScheduledTaskAction-执行“C:\WINDOWS\SysWOW64\WindowsPowerShell\v1.0\powershell.exe”-文件“C:\directory\powershell.ps1”
#第二个命令使用New-ScheduledTaskTrigger cmdlet将触发器变量$T分配给值AtLogon
$T=新的计划任务装配工-每周-每周星期六-上午7点
#第三个命令使用New-ScheduledTaskSettingsSet cmdlet将设置变量$S分配给任务设置对象
$S=新的ScheduledTaskSettingSet-AllowsArtifonBatteries-DontStopIfGoingOnBatteries-DontStopOnNidleEnd-ExecutionTimeLimit 1:00:00-MultipleInstances 2
#第四个命令将主变量分配给计划任务的新ScheduledTaskPrincipal,Domain\Username
$P=New ScheduledTaskPrincipal-UserId domain\gMSAaccount$-LogonType Password-RunLevel Highest
#第五个命令将任务定义的description变量设置为$D
$D=“在此处插入您的描述”
#注册新的计划任务并使用$D变量定义它
注册任务的ScheduledTask名称\u任务-操作$A-触发器$T-设置$S-主体$P-描述$D


我发现在active directory环境中使用gMSA帐户是最好的
要求是,为了使用gMSA,必须使用powershell脚本或schtask.exe创建任务。

看起来是正确的,谢谢,伙计。但它不会启动我的应用程序(我尝试使用“application.exe”字符串而不是“C:\Windows\notepad.exe”,但它也不起作用),只是排除了参数。。(我用它来设置在后台运行的隐藏进程。例如,$action=New ScheduledTaskAction-Execute'notepad.exe',它看起来是正确的,谢谢,伙计。但是它并没有启动我的应用程序(我试图使用而不是'application.exe'字符串'C:\Windows\notepad.exe',但它也不起作用)只是排除参数。)。。(我用它来设置在后台运行的隐藏进程。例如,$action=New ScheduledTaskAction-Execute'notepad.exe'