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>"
Advanced Sensor in PRTG anlegen
Beispiel der Parameter für das Script:
-ComputerName myComputer.lan.local -TaskName "myTaskName"
Sensor Einstellungen:
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.
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
awsome script! thanks for sharing!
Thanks Olson.
Hi , thanks for this script i need this very much…
i have a problem : i’m getting this error “ XML: The returned XML does not match the expected schema. (code: PE233) — JSON: The returned JSON does not match the expected structure (Invalid JSON.). (code: PE231) “
Can you ,please , help me ?
Hello,
There are a lot of Troubleshooting Suggestions in the comments of the article http://mycloudrevolution.com/2016/03/21/veeam-prtg-sensor-reloaded/.
If that does not solve your problem please let me know.
Kind regards
Markus
Thanks…
I have found the problem : between – and taskname there was a space , now it’s working ok .
Hi,
can you please help me about this :
Task Result 0 (configured lookup ptf.scheduledtask.taskresult is empty or not available)
Task Result Raw 0 #
Task State 267008 (configured lookup ptf.scheduledtask.taskresult is empty or not available)
Why I am getting this ? other way it’s working ok…
Hello,
Has the task you want to check ever run?
What mens: other way it is working ok?
Kind regards
Markus
in PRTG it is „green“ , the task that i’m monitoring have data in task scheduler history …
Sorry, I can not help you with the Troubleshooting without more details.
Might you please run the Script like in my Example with -Debug – verbose and send me all output?
Hi , this is the output :
„LastRunInHours
2
TimeHours
1
1
LastTaskResult
0
All
1
1
0
0
1
ComputerName Name Path Enabled State LastRunTime NextRunTime LastRunI
nHours
———— —- —- ——- —– ———– ———– ——–
ddlar——— Sam—- Sam——- True Ready 26.05.2017. 08:19:11 27.05.2017. 03:17:00 2
If you compare my example Screenshot and your result you can see that ‚LastTaskResult‘ is missing int output.
That is a problem with one of the PS-Functions…
No Verbose Messages during your execution?
Might you please give me some more details about your environment?
– OS of Server with Task
– OS of Server with Script
– PS Versions
both servers are on windows server 2012r2 , ps version 3
Never used that with PS v3…
ok, what is the version that you use ?
V4
Hi , I have upgraded my ps to V4 and now it’s ok .
Thanks for your time.
You are welcome.
Hi, the script looks really useful, but it would appear that the latest version of PRTG ( 19.2.50.2842) doesn’t understand the output of the command. I can run it no problem from the command line, but get the following when running it in PRTG
Response not well-formed: „( LastRunInHours 14189 TimeHours 1 1 LastTaskResult 0 All 1 1 0 0 1 )“ (code: PE132)
It’s ok, I figured it out. Was running it as an EXE custom instead of an XML Custom EXE.
HI getting error when trying to monitor a task with PRTG.
I tried to run your script on the machine where i want to monitor tasks and it works well.
i tried to run it remotely from our PRTG server to that same machine we want to monitor task and i’m getting error at line 52.
VERBOSE: Creating Object…
VERBOSE: Connect to Comuter and get Task Folders…
The request is not supported. (Exception from HRESULT: 0x80070032)
At C:UsersadmsimvoyDesktopPRTG-ScheduledTask.ps1:52 char:13
+ if(($Folders = $folderRef.getfolders(1))) {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
I tried to run localy the script on our PRTG server and it works well to monitor a task. can you help me?
I know it was working a few version before we updated PRTG.
Nevermind. i Found out that the Firewall wasn’t configured correctly for Remote Task Management on the server I wanted to monitor tasks. I don’t know why… but it is now working…
Hello,
Your pluging seems to be exactly what we’re looking for. We tried to test it on W10 and 2k12 srv by launching manually the script into a PS console but we’ve this error message about special caracter :
Ampersand not allowed. The & operator is reserved for future use; use „&“ to pass ampersand as a string
We also have error for „<" caracter.
Is there any option or someting to enable or disable for that ? Or Do we have to had "" after and before the caracters ?
Tks in advance.
Romain
Hello Romain,
might yoiu please share the full command you habe executed, e.g.
PRTG-ScheduledTask.ps1 -ComputerName myComputer.lan.local -TaskName "myTaskName" -Debug -Verbose
?Great sensor, we use it really often in our company.
Sadly we have experienced difficulties lately:
On many of our task servers the “Missed Runs” channel triggers red now and then. Not every run but occasionally.
We found out that it happens only on Server 2012R2 so far. The task (eg. start time 10:00:00 am) sometimes runs a few milliseconds too early (eg. 9:59:59:531). Some googling revealed that it is a multi-core phenomenon. Core times can be off by a few milliseconds, so one core is at 10:00, triggers the start and the second core that runs the task loggs the start time with it’s own core time (eg. 9:59…).
Is there any way your sensor could respect this? Because basically the job ran, but the channel is red for the whole day.
Thanks Alex.
Does the task manager on the the affected server show the run as failed when this issue occurs?
Hello Markus
I have a stupid Question. Where can i download the PRTG Scheduled Task Script? I work at the moement with the scheduledTask2XML from Paessler, but i wan’t to have the ability to change the error or warning message.
Thank.
Regards Thomas
Hello,
the integration of GitHub Gist was broken. Sorry about that. The article is now up to date with the latest version. You can also check this GitHub Repo for other Scripts: https://github.com/vMarkusK/Advanced-PRTG-Sensors
Perfect.. 🙂
Thanks for the amazing Script. Very helpfully…
greets
Thomas
Nice script! With newer versions of PRTG however there is a problem with the ‚Write-Host‘ command. This gives an error „A command that prompts the user failed because the host program or the command type does not support user interaction“ in the script output when PRTG runs it. Not when you manually run it in a Powershell.
Replacing ‚Write-Host‘ with ‚Write-Output‘ solves the issue. Or even leaving the command out of it and just outputting your XML text like this also works:
if ($myTask) {
„“
foreach ($Object in $myTask){
$LastRunInHours = $Object.LastRunInHours
$LastTaskResult = $Object.LastTaskResult
$LastRunTime = $Object.LastRunTime
$NextRunTime = $Object.NextRunTime
„“
„LastRunInHours“
„$LastRunInHours“
„TimeHours“
„1“
„1“
„“
„“
„LastTaskResult“
„$LastTaskResult“
„All“
„1“
„1“
„0“
„0“
„1“
„“
}
„“
}
Greetings,
Steven.
Hi Steven,
thanks for the feedback. The latest version of the Script has already implemented this change:
https://github.com/vMarkusK/Advanced-PRTG-Sensors/blob/master/Veeam/PRTG-VeeamBRStats.ps1
Best regards
Markus
Hi.
I’m trying to use Advanced-PRTG-Sensor for custom scheduled tasks on Windows server, but I’m getting error: „XML: The returned XML does not match the expected schema. (code: PE233) — JSON: The returned JSON does not match the expected structure (Invalid JSON.). (code: PE231)“. after checking with PRTG support, they said: „From first look it seems fine. The only thing I noticed is that you defined for LimitMaxError and LimitMinError the same values. Please use different values since this will cause an issue.“
What is the purpose of those values, how do they reflect to a sensor and should they be changed?
Thanks
Hi McBarkinjo,
The error you see in PRTG is a generic error in all cases of an error with the return structure (XML) of Advanced Script sensors. Since this script/project has not been maintained for a long time, I would recommend using the built-in sensor for scheduled tasks: https://www.paessler.com/manuals/prtg/windows_scheduled_task_sensor
Best regards
Markus
Do you still support this solution? I’m having issues configuring probe on prtg server but getting access denied when trying to monitor tasks on a remote server.
Hi,
This project was not maintained for a while. Might be better to use the build in scheduled task sensor.
The prtg built in scheduled task sensor was depreciated in 2016. Thank you for reply Markus.
The built in prtg sensor for sch tasks was depreciated in 2016/17 it seems. Trying to find other solutions when I stumbled upon this thread. Thank you for your quick reply.
Senser:
ScheduledTask-scnsza6017
响应结构错误:“( LastRunInHours 528 TimeHours 1 1 LastTaskResult 0 All 1 1 0 0 1 )“(代码:PE132)
1:According to your script (PRTG-ScheduledTask.ps1), adding sensors inside prtg generates the following error, but testing your script under Powershell is OK.
2: I don’t know how you add this within Prtg((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:
„0“
„0“
„1“))
wait for your response
Thanks!
PE132 indicates a format error in the return of the script. This can have a broad range of issues. Please enable and check the Log of the sensor for more details.
Hello,
what is the minimum permission the service user needs for this sensor? I do not want to give local admin rights on the server we want to monitor.
Thank you.
BR
Hi Jana,
this post/script has not been maintained for a while now. Unfortunately, I can not say anything about this.
Best Wishes
Markus