0

I want to create a scheduled task using a gMSA account via powewershell but I get some errors when I try to register the task. Below you can see my code and the error:

$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

This is the error:

 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
4
  • I think this -User $user is meant to be <string> and you're passing an object as argument. Commented Jun 8, 2021 at 13:50
  • Try removing the Trigger, create the task without any trigger. I think the error could be related to it. Commented Jun 8, 2021 at 14:49
  • Hello, i hvae removed the trigger and the task was created, but i can not add manually the trigger becasues a passowrd is requested sice I use gmsa i don't know the password. Also i saw that the task doesn't work properly, it is on running state but nothing is happening. I think that the task trigger a notepad instead of .ps1 file
    – bogdan09
    Commented Jun 8, 2021 at 15:42
  • hello, i have created the task, i saved the script under "C" drive and when i run it from that path the thak was created. Thank you for you time!
    – bogdan09
    Commented Jun 9, 2021 at 13:19

1 Answer 1

2

Since you've already configured a logon principal for the task, use the -Principal parameter instead of -User:

Register-ScheduledTask -TaskName "test" -Trigger $trigger -Action $action -Principal $user
3
  • i have changed the "-user" with "-principal " and i receive the same error
    – bogdan09
    Commented Jun 8, 2021 at 14:00
  • 1
    @bogdan09 Did you check if the gMSA is ready to be used on your current computer? Mathias answer is correct and should work just fine. Commented Jun 8, 2021 at 14:08
  • Hello, yes i checked , the output of this command "test-adserviceaccount mygmsa" is "true", also my server is member in that specified group and it si able to retreive the gmsa password
    – bogdan09
    Commented Jun 8, 2021 at 14:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.