We need to monitor a scheduled task created on a server can we do it by PRTG?


Article Comments

Daniel,

We cannot monitor the scheduled task itself natively. You might be best by creating a PowerShell script to poll for the data, and have that dump into PRTG via a custom script.

Depending on what the task does, we can monitor for services or processes it invokes.

Benjamin Day
Paessler Support


Aug, 2018 - Permalink

Hi,

For me, it was a pleasant surprise https://mycloudrevolution.com/2016/09/15/prtg-advanced-scheduled-task-sensor/


Jul, 2019 - Permalink

Here's a shorter PowerShell script I created based on the details in the link above.

# Parameters
param (
  [parameter(Mandatory=$true,position=0)] 
  [string] 
  $Server,

  [parameter(Mandatory=$true,position=1)] 
  [string] 
  $TaskName,

  [parameter(position=3)] 
  [string] 
  $Folder = '\'
)


# Main

try {
    # Get task status
    $Schedule = New-Object -ComObject 'Schedule.Service'
    $Schedule.connect($Server)
    $FolderRef = $Schedule.getfolder($Folder)
    $Task = $FolderRef.GetTasks(1) | Where-Object {$_.name -eq $TaskName}

    $TaskEnabled = $Task.enabled
    $TaskLastRunHours = [math]::round($(New-TimeSpan -Start $([datetime]$Task.lastruntime) -End $(Get-Date)).TotalHours,0)
    $TaskLastResult = $Task.lasttaskresult

    # Create PRTG response
    $Message = "The scheduled task `'$TaskName`' last ran $TaskLastRunHours hour(s) ago with a result of $TaskLastResult."

    $Channels = @()

    $Channels += [pscustomobject]@{
        'channel'="Task Enabled";
        'value'= [int]($TaskEnabled)
    }

    $Channels += [pscustomobject]@{
        'channel'="Hours since last run";
        'value'= $TaskLastRunHours
    }

    $Channels += [pscustomobject]@{
        'channel'="Last run result";
        'value'= $TaskLastResult
    }

    $Result = [pscustomobject]@{
        result = $Channels;
        text = "$Message"
    }
    $PRTGResult = [pscustomobject]@{prtg = $Result}
    $PRTGResult | ConvertTo-Json -Depth 3
}
catch {
    $Result = [pscustomobject]@{
        error = '1';
        text = "$HealthCheckURL Error at line $($_.InvocationInfo.ScriptLineNumber): $_     $($_.Exception)"
    }
    $PRTGResult = [pscustomobject]@{prtg = $Result}
    $PRTGResult | ConvertTo-Json
}

Oct, 2019 - Permalink

Dezdez,

Have you considered submitting this for ScriptWorld?

Submit your script to ScriptWorld

Thanks!

Benjamin Day
Paessler Support


Oct, 2019 - Permalink