3
votes

I have several hundred scheduled tasks. Im looking for a way to get the name of the task that called a powershell script when the script has been executed by the task scheduler. So for instance I could include that name in an alert I am sending so we know not only which server but which specific task

2

2 Answers

0
votes

You can use the get-scheduledtask cmdlet. You find this cmdlet in the ScheduledTasks module.

Each returned task has an Actions Property.

E.g.

(Get-ScheduledTask)[0].Actions
0
votes

To add on to Peter Schneider helpful answer which will give you details on the first task in the list. So, more specifically, to get them all, try this...

(((Get-ScheduledTask).Actions) | Select-Object *) -match 'powershell' | Select-Object -Property Id,Execute,Arguments | Format-Table -Wrap

Id                 Execute        Arguments
--                 -------        ---------
StartPowerShellJob powershell.exe -NoLogo -NonInteractive -WindowStyle Hidden -Command "Import-Module PSScheduledJob; …

Note, the '*' gest all properties and you may not want to do that, so, just explicitly select the ones you'd want

(((Get-ScheduledTask).Actions) | Select-Object -Property Id,Execute,Arguments) -match 'powershell' | Format-Table -Wrap