cancel
Showing results for 
Search instead for 
Did you mean: 
Create Post

Monitor Windows Scheduled Tasks under Main Task Library and other Sub-Folders

Hello Guys,

This is about monitoring the tasks scheduled in Windows Platform. Though there is a by-default monitoring available in SAM for the Tasks (when we do List Resource - it appears). But there is a limitation too.

pastedImage_0.png

The Limitation is - Solarwinds provides Task Monitoring feature only for the tasks under  "Task Scheduler Library Folder (the parent folder)" only, not the other tasks under sun-folders (the child folders).

1. Tasks under Library Folder.

pastedImage_1.png

2. Tasks under Child Folder.

pastedImage_3.png

To monitor the scheduled tasks which are under sub-folders, I have written two scripts by taking reference from other developers post. These scripts will gather all the tasks name with status details and will store the result on a local path which is defined in the script (can be modified according to need). This script will bring both Parent Library Folder Tasks as well as the sub-folder tasks i.e. all scheduled jobs running on a Windows machine.

Results of the Script will give the number of tasks for each state as below-

1. Ready

2. Running

3. Could Not Start

4. Unknown

5. Others

pastedImage_4.png

Reason for two scripts, details are as below -

Script -1 : This is based on "Get-ScheduledTask" cmdlet, which runs on new Windows platform coming with by-default Powershell. Some times like in older versions of Windows i.e. 2003 or 2008 that cmdlet is not recognized. For this reason I have created another script with different cmdlet, as below.

Script -2 : This is based on "schtasks.exe" cmdlet, which supports on the older versions of Windows as mentioned above to get the scheduled tasks details.

Based on the various statistics parameters value , you can create the alerts according to the requirement.

Note: The tasks details as mentioned above will be saved in ".csv" format with the filename based on the date and time the script will be executing. And that file will be stored on the local machine where the script is executed. So a user can go and check the historical data, even they can build the dashboard based on those files, if needed.

Attachments
Comments

I wanted to add to this as I have a need to monitor scheduled tasks in SolarWinds and the built in functionality isn't sufficient. This code is not complete, but I think it can provide a good starting point for anyone else needing to do something similiar. You could turn it into a function and pass the tasks names you want to check for in as an argument, create a custom PSObject to gather properties from both $maincommand and $ScheduledTasks, etc....

Once I complete my monitor I'll probably create a post with the full breakdown.

# Getting variables ready

$FoundTask = @()

$BadTasks = @()

$GoodTasks = @()

[DateTime]$CurrentDate = Get-Date

$stat1=$stat2=0;

$mess1=$mess2="";

# Task names we want to check

$TaskNamesToCheck = 'Adamj Clean-WSUS' , 'Decline-PreviewUpdates'

# Try/Catch to see if we can get the scheduled tasks

Try {

    # Getting all scheduled tasks and excluding disabled ones

    $AllTasks = Get-scheduledTask | where-object {($_.State -ne "Disabled") -and (!$_.Hidden)}

}

    Catch {

        Write-Host "Message.BadTasks: $($Error[0])";

        Write-Host "Statistic.BadTasks: -1";

        Exit 1

    }

# Only getting ones that have a future run time

$ScheduledTasks = $AllTasks | Get-ScheduledTaskInfo | where-object {$_.NextRunTime -gt $CurrentDate}

# Filtering out the ones we wanted to check for

ForEach ($ThisTask in $ScheduledTasks) {

    ForEach ($ThisTaskToCheck in $TaskNamesToCheck) {

        $entry = $ThisTask | Where-Object {$_.TaskName -eq $ThisTaskToCheck}

        $FoundTask += $entry

    }

}

# Checking out our task results. Needs more testing to see if a task in a running state shows 0

ForEach ($ThisTask in $FoundTask) {

    switch ($ThisTask.LastTaskResult) {

        {$_ -eq '0'} `

            {$GoodTasks += $ThisTask}

        {$_ -ne '0'} `

            {$BadTasks += $ThisTask}

    }

}

# Populating our variables to write out in SolarWinds

$mess1 = $GoodTasks.TaskName -join ', '

$stat1 = ($GoodTasks | Measure-Object).count

$mess2 = $BadTasks.TaskName -join ', '

$stat2 = ($BadTasks | Measure-Object).count

# Writing the results out for SolarWinds

Write-Host "Message.GoodTasks: Tasks in a good state: $mess1";

Write-Host "Statistic.GoodTasks: $stat1";

Write-Host "Message.BadTasks: Tasks in a bad state: $mess2";

Write-Host "Statistic.BadTasks: $stat2";

# Hey, we made it this far. Let's give SolarWinds a happy exit code

exit 0

Thanks for your feedback and enhancement.

Hey mmorrison - was wondering if you ever got around to creating a full breakdown for this?

Thanks!

Version history
Revision #:
1 of 1
Last update:
‎04-20-2017 07:57 AM
Updated by: