使用powershell创建定时任务

Create a scheduled task with powershell

我想通过 powewershell 使用 gMSA 帐户创建计划任务,但在尝试注册任务时出现一些错误。您可以在下面看到我的代码和错误:

$action = New-ScheduledTaskAction  "mypath\myscript.ps1"
$trigger = New-ScheduledTaskTrigger -Weekly -At 12:00 
$user =  New-ScheduledTaskPrincipal -UserId domain\gmsa -LogonType Password -RunLevel Highest
Register-ScheduledTask -TaskName "test" -Trigger $trigger -Action $action -User $user

这是错误:

 Register-ScheduledTask : The parameter is incorrect. At line:1 char:1
 + Register-ScheduledTask -TaskName "test" -Trigger $trigger -Action ...
 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     + CategoryInfo          : InvalidArgument: (PS_ScheduledTask:Root/Microsof     t/...S_ScheduledTask)
 [Register-ScheduledTask], CimException
     + FullyQualifiedErrorId : HRESULT 0x80070057,Register-ScheduledTask

由于您已经为任务配置了登录主体,因此请使用 -Principal 参数而不是 -User:

Register-ScheduledTask -TaskName "test" -Trigger $trigger -Action $action -Principal $user