admin管理员组文章数量:1432378
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 badge1 Answer
Reset to default 2The 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
本文标签: windowsUpdating task scheduler entries to modify task argumentsStack Overflow
版权声明:本文标题:windows - Updating task scheduler entries to modify task arguments - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745602898a2665670.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论