最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

windows - Updating task scheduler entries to modify task arguments - Stack Overflow

matteradmin3PV0评论

I am attempting to iterate over many task scheduler entries to modify the task arguments. We have many servers running simultaneously launched scripts, all need to be reachable on the console if a process needs attention.

Ideal is to launch minimized with start /min or powershell -window minimized

I am testing against one sample task. My code so far looks like

$PREFIX = "-window minimized"
$taskPath = "\somepath\"
Get-ScheduledTask -CimSession "servername" -TaskPath $taskPath -TaskName "mytask" | ForEach-Object {

  $actions = $_.Actions
  $actions New-ScheduledTaskAction -Execute "powershell" -Argument "${PREFIX} ${actions}"
  Set-ScheduledTask -CimSession "servername" -TaskPath $taskPath -TaskName $_.TaskName -Action $actions
}

This works excepting I cannot get to the old command arguments. Always I get as new arguments with my prefix and then 'MSFT_TaskExecAction' following. This fails against a multi-step task resulting in only one bad step afterwards (and MSFT_TaskExecAction). Thanks all.

I am attempting to iterate over many task scheduler entries to modify the task arguments. We have many servers running simultaneously launched scripts, all need to be reachable on the console if a process needs attention.

Ideal is to launch minimized with start /min or powershell -window minimized

I am testing against one sample task. My code so far looks like

$PREFIX = "-window minimized"
$taskPath = "\somepath\"
Get-ScheduledTask -CimSession "servername" -TaskPath $taskPath -TaskName "mytask" | ForEach-Object {

  $actions = $_.Actions
  $actions New-ScheduledTaskAction -Execute "powershell" -Argument "${PREFIX} ${actions}"
  Set-ScheduledTask -CimSession "servername" -TaskPath $taskPath -TaskName $_.TaskName -Action $actions
}

This works excepting I cannot get to the old command arguments. Always I get as new arguments with my prefix and then 'MSFT_TaskExecAction' following. This fails against a multi-step task resulting in only one bad step afterwards (and MSFT_TaskExecAction). Thanks all.

Share Improve this question edited Nov 18, 2024 at 21:08 mklement0 443k68 gold badges712 silver badges930 bronze badges asked Nov 18, 2024 at 17:54 Stack PacketStack Packet 111 silver badge1 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 2

The Actions property contains a list of action instances - not just their arguments - for that you'll want the Arguments property on one of the contained actions:

$actions = $_.Actions |ForEach-Object {
  if ($_.Execute -match 'powershell') {
    # a powershell task, let's create a new one with the prefix
    $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
Post a comment

comment list (0)

  1. No comments so far