How do I figure out what's wrong with Register-ScheduledTask parameter sets?

Viewed 2280

I have a (unfortunately) very, very long PowerShell script that builds out an entire server instance. That is: all files are installed, all system settings are added, and so on. The last thing it does is add a scheduled task to run the "master" program when the computer boots. However, I'm getting an error from Register-ScheduledTask and I really can't see what's wrong. This is my FIRST PowerShell script.

I've played with passing different parameters, like not using -InputObject and using the individual subset objects. I've removed some of the extra parameters (like -Description, -Password, -RunLevel, etc) but always get the same result (except not passing any info gives an error that there is no action, of course).

$INSTALL = "C:\PPC\MCPSuite" # hardcoded for example
$TASKPWD = "1234567890" # ditto
$action = New-ScheduledTaskAction -Execute "${INSTALL}\MCPSuite.exe" -WorkingDirectory "$INSTALL"
$owner = New-ScheduledTaskPrincipal -UserId "dnpapps" -LogonType "Password" -RunLevel "Highest"
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -ExecutionTimeLimit (New-TimeSpan -Seconds 0) -Priority 4
$trigger = New-ScheduledTaskTrigger -AtStartup -RandomDelay (New-TimeSpan -Minutes 1)
$task = New-ScheduledTask -Action $action -Principal $owner -Trigger $trigger -Settings $settings
Register-ScheduledTask -TaskName "PPC-MCPSuite-Test" -Description "MCP Suite Manager" -InputObject $task -Password "$TASKPWD" -RunLevel Highest -Force

Here is the error message:

Register-ScheduledTask : Parameter set cannot be resolved using the specified named parameters.
At C:\Users\levy\MCP-Installer\MCPv2\task.ps1:14 char:1
+ Register-ScheduledTask -TaskName "PPC-MCPSuite-Test" -Description "MC ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Register-ScheduledTask], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,Register-ScheduledTask
1 Answers

The error message, Parameter set cannot be resolved using the specified named parameters. means that the parameters consist an incorrect combination. Let's see the docs.

You are using -InputObject, and it's defined in one of the four signatures available like so:

Register-ScheduledTask
        [-Force]
        [-InputObject] <CimInstance>
        [[-Password] <String>]
        [[-User] <String>]
        [[-TaskName] <String>]
        [[-TaskPath] <String>]
        [-CimSession <CimSession[]>]
        [-ThrottleLimit <Int32>]
        [-AsJob]
        [<CommonParameters>]

You are also using -Description, which doesn't exist in the signature above. However, it's available in two different signatures, neither of those support -InputObject. Since the arguments don't match, Powershell throws you the error above.

To fix the issue, call the cmdlet with valid set of parameters. Since -InputObject $task seems to contain task settings, pass the description within it. Maybe something like so,

$task = New-ScheduledTask -Action $action -Principal $owner -Trigger $trigger -Settings $settings -Description "MCP Suite Manager" 
Register-ScheduledTask -TaskName "PPC-MCPSuite-Test" -InputObject $task -Password "$TASKPWD" -RunLevel Highest -Force
Related