1

OS - Windows Server 2012

I have an inventory scheduled task that has 31 actions. Each action runs a different Powershell script. If one of the actions/Powershell scripts is stuck, I want to know which one it is so I know which one may have a coding issue.

Is there any way to indicate which action is running out of the 31? Is there a Powershell script that I can run where I enter the scheduled task name and it will return something like "Processing 18 or 31 actions"?

Something to add to this script:

Get-ScheduledTask |where TaskName -EQ 'Inventory_Part3'| Get-ScheduledTaskInfo 
Jeremy F.
  • 1,778
  • 11
  • 51
  • 86
  • Under the History Tab in Task Scheduler you can see which individual action has started and completed – HAL9256 Aug 28 '18 at 21:02

1 Answers1

0
  1. If the powershell.exe process is "hung" as in still running, you could find the action via the script name passed to the PowerShell.exe process by the task scheduling system.

    You'd need to use Get-WMIObject -ClassName Win32_Process to access the arguments passed to the process, as explained in answers to this question

  2. Answers to this question indicate where the operational history of the task scheduler is stored in the Windows Event log. Per-action log entries may be available and filterable by your own powershell script.

  3. Lower level WMI/CIM objects may contain more information than that exposed by the *-ScheduledTask cmdlets. You could explore classes like MSFT_ScheduledTask and MSFT_TaskAction with Get-CimClass.

    At first glance it doesn't seem like there is any kind of CurrentAction attribute, but further digging could unearth something usable.

The live status of your tasks should be visible through the Schedule.Service COM object. Unfortunately I'm limited to Linux right now, so I don't have access to a test environment.

In broad strokes:

  1. Here is a vbscript examples of querying the task scheduler service here. The Task Scheduler is exposed as a COM object Schedule.Service, which PowerShell should be able to manipulate.

  2. The TaskService object has a GetRunningTasks member that should return you a collection of...

  3. RunningTask objects, which have the CurrentAction property

I suspect the code would look something like this (but again, I have nowhere to test):

$TaskScheduleService = New-Object -comobject 'Schedule.Service'
$TaskScheduleService.Connect()
$RunningTasks = $TaskScheduleService.GetRunningTasks()
$RunningTasks | Where-Object {<#some kind of filter to get only your tasks#>} |
    Foreach-Object {
        $_.CurrentAction
    }
veefu
  • 2,820
  • 1
  • 19
  • 29