Search code examples
powershellscheduled-tasks

PowerShell says "The parameter is incorrect" when attempting to create a scheduled task


I am trying to make a script that creates a task in task scheduler that runs every 2 months at 7:00 PM that will run Dell Command Update, install updates, and reboot if necessary.

When running this script I just get an error about the parameter being incorrect (The parameter is incorrect.), with the category being: InvalidArgument.

Below is the code I am running.

$TaskName = "DellCommandUpdateTask"

$action = New-ScheduledTaskAction -Execute "C:\Program Files\Dell\CommandUpdate\dcu-cli.exe" -Argument "/scan -silent /applyUpdates -silent -reboot=enable"

$Trigger = New-ScheduledTaskTrigger -Weekly -WeeksInterval 8 -At "19:00"

Register-ScheduledTask -TaskName $TaskName -Trigger $Trigger -Action $action

I tried some different combinations of ' and " around the execute action.

I think the issue may be in the $action part of this code, but I am not sure.


Solution

  • The problem isn't your New-ScheduledTaskAction call, it is your New-ScheduledTaskTrigger call:

    You neglected to specify a day of the week (possibly multiple) on which to run your task:

    For instance, to run on Sundays, use -DaysOfWeek Sunday:

    $Trigger = 
      New-ScheduledTaskTrigger -Weekly -WeeksInterval 8 -At 19:00 -DaysOfWeek Sunday
    

    Your own attempt amounted to an incomplete scheduled-task trigger specification, and it is unfortunate that New-ScheduledTaskTrigger doesn't itself prevent that, resulting in an - obscure - error message only later, at the time of calling Register-ScheduledTask (The parameter is incorrect.)


    As an aside:

    • Because you didn't also pass a -Principal argument to your Register-ScheduledTask call, the task will run in the context of the calling user, and only when that user is logged on.

    • Use New-ScheduledTaskPrincipal to obtain a different task principal (user identity), which allows you to run the task independently of any given user and independently of whether someone is logged on or not.