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.