0
votes

I'm trying to remove schedule task on remote servers.

 Invoke-Command -ComputerName "name" {Unregister-ScheduledTask -TaskName $task -WhatIf}

I get the following error

Cannot validate argument on parameter 'TaskName'. The argument is null. Provide a valid value for the argument, and then try running the command again. + CategoryInfo : InvalidData: (:) [Unregister-ScheduledTask], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Unregister-ScheduledTask + PSComputerName : name

$Task='task'

If I specify the "Taskname" in text and not variable it works.

Invoke-Command -ComputerName "name" {Unregister-ScheduledTask -TaskName "task" -WhatIf}

What if: Performing operation 'Delete' on Target '\task'.

1

1 Answers

0
votes

To pass a named variable to a scriptblock, do:

Invoke-Command -ComputerName $Computer -ScriptBlock {param($task) Unregister-ScheduledTask -TaskName $task } -ArgumentList $TaskName

or use the $args Automatic variable like:

Invoke-Command -ComputerName $Computer -ScriptBlock { Unregister-ScheduledTask -TaskName $args[0] } -ArgumentList $TaskName