PRTG – Advanced Scheduled Task Sensor

Mit einer der letzten Versionen hat Paessler in PRTG den Scheduled Task Sensor endgültig eingestellt. In einem KB Artikel verweißt Peassler auf den ScheduledTask2XML sensor von PRTG Tools Family als Ersatz. Mir fehlt bei dem Tool allerdings etwas die Transparent und ich benötige nicht all diese Channels. Daher habe ich mir meinen eigenen PowerShell Advanced Sensor zur Scheduled Task Überwachung erstellt.

Scheduled Task Sensor in PRTG

In dem Beispiel ist dem Channel „LastRunInHours“ manuell ein Upper Warning und Error Level hinzugefügt. Der Channel „LastTaskResult“ wird automatisch mit den nötigen Error Levels konfiguriert.

Auszug aus der XML:

"<LimitMinError>0</LimitMinError>"
"<LimitMaxError>0</LimitMaxError>"
"<LimitMode>1</LimitMode>"

Scheduled Task Sensor in PRTG

Advanced Sensor in PRTG anlegen

Beispiel der Parameter für das Script:

-ComputerName myComputer.lan.local -TaskName "myTaskName"

Sensor Einstellungen:

Scheduled Task Sensor - Setup

Scheduled Task Sensor im Debug Mode

Der Debug und Verbose Modus ist nur zum Test auf dem Probe oder Core Server gedacht. Im PRTG Sensor diese Parameter bitte nicht verwenden.

Scheduled Task Sensor Debug Mode

PowerShell Script für den PRTG Advanced Sensor

<#
    .SYNOPSIS
    PRTG Advanced Scheduled Task Sensor
  
    .DESCRIPTION
    This Advanced Sensor will report Task Statistics.
        
    .EXAMPLE
    PRTG-ScheduledTask.ps1 -ComputerName myComputer.lan.local -TaskName "myTaskName"

    .Notes
    NAME:  PRTG-ScheduledTask.ps1
    AUTHOR: Markus Kraus
    LASTEDIT: 09/14/2016
    VERSION: 1.1
    KEYWORDS: PRTG, Windows, Schedule Task
   
    .Link
    http://mycloudrevolution.com/

    .Link
    https://vater.cloud
 
 #Requires PS -Version 3.0  
 #>
[cmdletbinding()]
param(
    [Parameter(Position=0, Mandatory=$false)]
    	[string]$ComputerName = $env:COMPUTERNAME,
	[Parameter(Position=1, Mandatory=$true)]
        [string]$TaskName
)

#region: Definitions
$Date = Get-Date -Format G
#endregion

#region: Functions
## Source: https://gallery.technet.microsoft.com/scriptcenter/Get-Scheduled-tasks-from-3a377294
function Get-AllTaskSubFolders {
    [cmdletbinding()]
    param (
        # Set to use $Schedule as default parameter so it automatically list all files
        # For current schedule object if it exists.
        $FolderRef = $Schedule.getfolder("\")
    )
    if ($FolderRef.Path -eq '\') {
        $FolderRef
    }
    if (-not $RootFolder) {
        $ArrFolders = @()
        if(($Folders = $folderRef.getfolders(1))) {
            $Folders | ForEach-Object {
                $ArrFolders += $_
                if($_.getfolders(1)) {
                    Get-AllTaskSubFolders -FolderRef $_
                }
            }
        }
        $ArrFolders
    }
}
#endregion Functions

#region: Create Object
try {
    Write-Verbose "Creating Object..."
	$Schedule = New-Object -ComObject 'Schedule.Service'
} catch {
	Write-Warning "Schedule.Service COM Object not found, this script requires this object"
	return
}
#endregion

#region: Connect to Comuter and get Task Folders
Write-Verbose "Connect to Comuter and get Task Folders..."
$Schedule.connect($ComputerName) 
$AllFolders = Get-AllTaskSubFolders
#endregion

#region: Get Task Details
Write-Verbose "Get Task Details..."
[Array] $myTask = $AllFolders.GetTasks(1) | Where-Object {$_.name -eq $TaskName} | Foreach-Object {
	        New-Object -TypeName PSCustomObject -Property @{
	            'Name' = $_.name
                'Path' = $_.path
                'State' = switch ($_.State) {
                    0 {'Unknown'}
                    1 {'Disabled'}
                    2 {'Queued'}
                    3 {'Ready'}
                    4 {'Running'}
                    Default {'Unknown'}
                }
                'Enabled' = $_.enabled
                'LastRunTime' = $_.lastruntime
                'LastRunInHours' = [math]::round($(New-TimeSpan -Start $([datetime]$_.lastruntime) -End $Date).TotalHours,0)
                'LastTaskResult' = $_.lasttaskresult
                'NumberOfMissedRuns' = $_.numberofmissedruns
                'NextRunTime' = $_.nextruntime
                'ComputerName' = $Schedule.TargetServer
            }
    }
if ($myTask.Length -gt 1) {
        Write-Error "More than one Task fount. Exiting..."
        Exit 1
}
#endregion

#region: XML Output
Write-Verbose "XML Output..."
if ($myTask) {
    Write-Host "<prtg>" 
    foreach ($Object in $myTask){
        $LastRunInHours = $Object.LastRunInHours
        $LastTaskResult = $Object.LastTaskResult
        $LastRunTime    = $Object.LastRunTime
        $NextRunTime    = $Object.NextRunTime
        Write-Host "<result>"
                    "<channel>LastRunInHours</channel>"
                    "<value>$LastRunInHours</value>"
                    "<Unit>TimeHours</Unit>"
                    "<showChart>1</showChart>"
                    "<showTable>1</showTable>"
                    "</result>" 
        Write-Host "<result>"
                    "<channel>LastTaskResult</channel>"
                    "<value>$LastTaskResult</value>"
                    "<DecimalMode>All</DecimalMode>"
                    "<showChart>1</showChart>"
                    "<showTable>1</showTable>"
                    "<LimitMinError>0</LimitMinError>"
                    "<LimitMaxError>0</LimitMaxError>"
                    "<LimitMode>1</LimitMode>"
                    "</result>" 
            }
    Write-Host "</prtg>" 
}
else {
    Write-Error "No Task with Name: $TaskName found. Exiting..."
    Exit 1
}
#endregion

#region: Debug
if ($DebugPreference -eq "Inquire") {
	$myTask | Select-Object ComputerName, Name, Path, Enabled, State, LastRunTime,  NextRunTime,  LastRunInHours, LastTaskResult, NumberOfMissedRuns | ft * -Autosize
}
#endregion

28 Comments

  1. olson 12. Mai 2017
  2. Kristijan 24. Mai 2017
  3. Kristijan 25. Mai 2017
    • Markus Kraus 25. Mai 2017
  4. Kristijan 25. Mai 2017
    • Markus Kraus 25. Mai 2017
      • Kristijan 26. Mai 2017
        • Markus Kraus 26. Mai 2017
          • Kristijan 26. Mai 2017
          • Markus Kraus 26. Mai 2017
  5. Kristijan 26. Mai 2017
  6. k 29. Mai 2017
  7. Dominic Winter 16. September 2019
  8. Dominic Winter 16. September 2019
  9. Simon-Pier 8. Oktober 2019
  10. Simon-Pier 8. Oktober 2019
  11. Romain 17. Februar 2020
    • Markus Kraus 18. Februar 2020
  12. Axel 10. September 2020
    • Markus Kraus 10. September 2020
  13. Thomas 8. Oktober 2020
  14. Thomas 12. Oktober 2020

Leave a Reply