Iterating over task scheduler tasks updating task arguments

My boss wants to launch our task scheduler tasks minimized., I was attempting to iterate over tasks with multiple steps, you can see I am not very powershell literate but do understand some constructs as similar to perl.

I ended up with code as below. Resulting in an error, ForEach-Object : A parameter cannot be found that matches parameter name ‘Arguments’.

    $PREFIX = "-window minimized "
    $taskPath = "\somepath\"
    Get-ScheduledTask -CimSession "servername" -TaskPath $taskPath -TaskName "mytask" | ForEach-Object {
      $actions = $_.Actions |ForEach-Object {
      if ($_Execute -match 'powershell') {
        $newTaskActionArgs = @{ Execute = 'powershell'; Arguments = "${PREFIX} $($_.Arguments)" }
        if ($_.WorkingDirectory) { 
          # make sure to copy the initial working directory if set too
          $newTaskActionArgs['WorkingDirectory'] = $_.WorkingDirectory
        }
        New-ScheduledTaskAction @newTaskActionArgs
      }
      else {
        # leave action as-is
        $_
      }
    }
    Set-ScheduledTask -Action $actions
    
    }

TIA for input.

Hi, welcome to the forum :wave:

May just be copy/paste errors but you do have some incorrect syntax in your code, e.g. this

if ($_Execute -match 'powershell')

should be:

if ($_.Execute -match 'powershell')

However, your problem with the unrecognised parameter name is because you’ve specified Arguments (plural). The parameter is the singular Argument.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.