Ways to monitor windows scheduled tasks
Ways to monitor windows scheduled tasks
Is there a way for KS-Soft to monitor Windows Scheduled Tasks?
Many Thanks,
Ryan
Many Thanks,
Ryan
-
- Posts: 2832
- Joined: Tue May 16, 2006 4:41 am
- Contact:
I think, you may use "WMI" test method: http://www.ks-soft.net/hostmon.eng/mfra ... ts.htm#wmi
Using "WMI" test method you may use "Win32_ScheduledJob" class to query necessary information regarding Windows Scheduled Tasks.
http://msdn2.microsoft.com/en-us/library/aa394399.aspx
Regards,
Max
Using "WMI" test method you may use "Win32_ScheduledJob" class to query necessary information regarding Windows Scheduled Tasks.
http://msdn2.microsoft.com/en-us/library/aa394399.aspx
Regards,
Max
-
- Posts: 2832
- Joined: Tue May 16, 2006 4:41 am
- Contact:
I am sorry. I have not noticed this restriction. In such case you may use "schtasks" utility to manage tasks, created by "Scheduled Tasks". "schtasks.exe" is a powerful command line utility, that might be used with "Shell script" test method: http://www.ks-soft.net/hostmon.eng/mfra ... m#chkShellRyan wrote:But the WMI only works for Jobs created using AT command, how about jobs created with the Windows Scheduled Task?
For instance, you may use following syntax to retrieve all necessary information about task, named "CertainTask" (of cource, you should use the name of the task you want to monitor):
schtasks /query /v | find "CertainTask"
So, you have to write simple bat file, that using "for" batch utility parses output of the "schtasks" utility.
Regards,
Max
-
- Posts: 2832
- Joined: Tue May 16, 2006 4:41 am
- Contact:
Hm. Windows 2000 doesn't have an equivalent command-line utility. Probably, jt.exe utility will help you: http://www.jsifaq.com/SF/Tips/Tip.aspx?id=2621Ryan wrote:Correct me if I'm wrong, but that only seem to work for Windows 2003 and above, but I'm running Windows 2000 server.
Another solution I found, is a slightly "hack" a "schtasks" utility. Here is some information, that might be helpful, but I am not sure it works:
http://www.windowsitpro.com/Article/Art ... 25186.html
http://makemsi-manual.dennisbareis.com/schtasks_exe.htm
http://home.aol.com/bergert/w2k/tip23.htm
Regards,
Max
-
- Posts: 10
- Joined: Wed Jul 12, 2017 7:56 am
Did you get this to work Ryan? My goal is to have a script to query a specific task on a remote server and see if the scheduled task last run was succesful.
CMD looks like this. But can't get it to work in a shell script. Or is there another way to monitor this?
"SchTasks /query /s backup /FO list /V /TN "NAS-GebouwA Cleanup" | find "Result"
CMD looks like this. But can't get it to work in a shell script. Or is there another way to monitor this?
"SchTasks /query /s backup /FO list /V /TN "NAS-GebouwA Cleanup" | find "Result"
-
- Posts: 10
- Joined: Wed Jul 12, 2017 7:56 am
Hi Alex,
I should learn to give all the information in advance
1) Windows Server 2008 R2 or Windows Server 2012
2) Test status returns OK but reply is not filled in.
3) Sure (see beow)
4) i'm trying to execute directly
My goal is to see if the scheduled task ran succesfully
I should learn to give all the information in advance

1) Windows Server 2008 R2 or Windows Server 2012
2) Test status returns OK but reply is not filled in.
3) Sure (see beow)
4) i'm trying to execute directly
Code: Select all
Option Explicit
On Error Resume Next
const statusUnknown = "scriptRes:Unknown:"
const statusOk = "scriptRes:Ok:"
const statusBad = "scriptRes:Bad:"
Dim objArgs, server, task, strCommand, strResult
Set objArgs = WScript.Arguments
If objArgs.Count <> 2 then
WScript.StdOut.Write statusUnknown & "script requires 2 parameters <Server> <Naam van scheduled task>"
WScript.Quit
end If
server = objArgs(0)
task = objArgs(1)
strCommand = """SchTasks /query /s " & server & " /FO list /V /TN """ & task & """ | find ""Result"""
Set objShell = WScript.CreateObject("WScript.Shell")
strResult = oShell.run(strCommand, 1, True)
If strResult = 0 Then
WScript.StdOut.Write statusOk & strResult
Else
WScript.StdOut.Write statusBad & strResult
End If
What exactly output do you see?i'm trying to execute directly
There are errors in the script, cscript will tell you where exactly is syntax error
I think you have problem with quotation marks
strCommand = """SchTasks /query /s " & server & " /FO list /V /TN """ & task & """ | find ""Result"""
I would try
strCommand = "SchTasks /query /s " & server & " /FO list /V /TN " & task & " | find 'Result'"
Also there is non-syntax error
strResult = oShell.run(strCommand, 1, True) - return error code
If you need command output, you should use command like
oShell.Exec(strCommand).StdOut.ReadAll()
Regards
Alex
-
- Posts: 10
- Joined: Wed Jul 12, 2017 7:56 am
Hi Alex,
This is the output I see with the tester
The reply should be the Las result of the scheduled task "Last Result: 0"
The result doesn't change with you pointers on the script.
Found somewhere that ReadAll and Write can't be used in the same script. Sounded strange.
This is the output I see with the tester
Code: Select all
[14:50:21] Script executed, correct result received:
----------
- Status: Ok
- Reply:
----------
The result doesn't change with you pointers on the script.
Found somewhere that ReadAll and Write can't be used in the same script. Sounded strange.
>Found somewhere that ReadAll and Write can't be used in the same script. Sounded strange.
Why not?
You just need to wait when external command finished, e.g.
e.g.
and as I see this works fine with various commands but does not work with SchTasks, somehow oExec.Status keeps Status==0 (running) forewer.
Not sure why, yet
Regards
Alex
Why not?
You just need to wait when external command finished, e.g.
e.g.
Code: Select all
Set oExec = oShell.Exec(strCommand)
Do While oExec.Status = 0
WScript.Sleep 100
Loop
strResult = oExec.StdOut.ReadAll()
Not sure why, yet
Regards
Alex
-
- Posts: 10
- Joined: Wed Jul 12, 2017 7:56 am
I changed the script. As .vbs file it works like a charm. But when I change it from
to
it times-out. Even at +120 seconds. the cscript process keeps the CPU occupied untill i kill it.
Code: Select all
WScript.Echo strLinet
Code: Select all
WScript.StdOut.Write statusOk & strLine
Code: Select all
Set objShell = CreateObject("WScript.Shell")
strCommand = "SchTasks /query /s backup /FO list /V /TN ""Nas-GebouwA Cleanup"""
Set objWshScriptExec = objShell.Exec(strCommand)
Set objStdOut = objWshScriptExec.StdOut
While Not objStdOut.AtEndOfStream
strLine = objStdOut.ReadLine
If InStr(strLine,"Last Result:") Then
WScript.Echo strLine
End If
Wend
Actually we have our program that can check scheduled tasks and designed for Shell Script test method but it does not work well on Windows 2008/2012 (cannot get list of tasks).
We tried some script using Schedule.Service object - same result: works fine on Windows 2003 but does work on Windows 2008.
Microsoft implemented new API in Windows 7/2008, old API exists but not fully functional.
Sounds like task for HostMonitor version 11.
If you need this test right now, may be we find some temporary solution.
Regards
Alex
We tried some script using Schedule.Service object - same result: works fine on Windows 2003 but does work on Windows 2008.
Microsoft implemented new API in Windows 7/2008, old API exists but not fully functional.
Sounds like task for HostMonitor version 11.
If you need this test right now, may be we find some temporary solution.
Regards
Alex
-
- Posts: 10
- Joined: Wed Jul 12, 2017 7:56 am
Hi Alex,
Looking forward to version 11.
I had a 'revelation' this weekend and rewrote the thing in Powershell. It works now. Just tought I might share it with the community.
Hint: Params: <ServerName> <TaskName>
Use single quotes for parameters when they contain special characters and or spaces.
Looking forward to version 11.
I had a 'revelation' this weekend and rewrote the thing in Powershell. It works now. Just tought I might share it with the community.
Hint: Params: <ServerName> <TaskName>
Use single quotes for parameters when they contain special characters and or spaces.
Code: Select all
$statusUnknown = "ScriptRes:Unknown:"
$statusOk = "ScriptRes:Ok:"
$statusBad = "ScriptRes:Bad:"
if (!$args[0]) {
echo $statusUnknown"ServerName parameter is not specified"
exit
}
if (!$args[1]) {
echo $statusUnknown"TaskName parameter is not specified"
exit
}
$ServerName = $args[0]
$TaskName = $args[1]
$result = (schtasks /query /s $servername /FO LIST /V /TN $taskName | findstr "Result")
$result = $result.substring(12)
Try{
$lastResult = $result.trim()
}
Catch{
echo $statusUnknown"Please check parameters again! Unknown Exception!"
exit
}
if ($lastResult -eq 0) {
echo $statusOk$lastResult
} else {
echo $statusBad$lastResult
}