View previous topic :: View next topic |
Author |
Message |
Ryan
Joined: 10 Dec 2006 Posts: 7
|
Posted: Thu Jun 14, 2007 10:15 pm Post subject: Ways to monitor windows scheduled tasks |
|
|
Is there a way for KS-Soft to monitor Windows Scheduled Tasks?
Many Thanks,
Ryan |
|
Back to top |
|
|
KS-Soft Europe
Joined: 16 May 2006 Posts: 2832
|
|
Back to top |
|
|
Ryan
Joined: 10 Dec 2006 Posts: 7
|
Posted: Fri Jun 15, 2007 3:48 am Post subject: |
|
|
Thanks for prompt reply,
But the WMI only works for Jobs created using AT command, how about jobs created with the Windows Scheduled Task?
Many Thanks,
Ryan |
|
Back to top |
|
|
KS-Soft Europe
Joined: 16 May 2006 Posts: 2832
|
Posted: Fri Jun 15, 2007 4:21 am Post subject: |
|
|
Ryan wrote: | But the WMI only works for Jobs created using AT command, how about jobs created with the Windows Scheduled Task? | 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/mframe.htm#tests.htm#chkShell
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 |
|
Back to top |
|
|
Ryan
Joined: 10 Dec 2006 Posts: 7
|
Posted: Fri Jun 15, 2007 4:32 am Post subject: |
|
|
Thanks Max,
Correct me if I'm wrong, but that only seem to work for Windows 2003 and above, but I'm running Windows 2000 server. |
|
Back to top |
|
|
KS-Soft Europe
Joined: 16 May 2006 Posts: 2832
|
|
Back to top |
|
|
HelpdeskIVAGO
Joined: 12 Jul 2017 Posts: 10
|
Posted: Thu Sep 21, 2017 2:02 am Post subject: |
|
|
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" |
|
Back to top |
|
|
KS-Soft
Joined: 03 Apr 2002 Posts: 12801 Location: USA
|
Posted: Thu Sep 21, 2017 5:01 am Post subject: |
|
|
1) Windows version?
2) what exactly means "can't get it to work"? what is test status? Reply field?
3) may we see your script?
4) try to start script from command line without using /B option and check output for errors
Regards
Alex |
|
Back to top |
|
|
HelpdeskIVAGO
Joined: 12 Jul 2017 Posts: 10
|
Posted: Thu Sep 21, 2017 5:31 am Post subject: |
|
|
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
Code: |
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 |
My goal is to see if the scheduled task ran succesfully |
|
Back to top |
|
|
KS-Soft
Joined: 03 Apr 2002 Posts: 12801 Location: USA
|
Posted: Thu Sep 21, 2017 6:45 am Post subject: |
|
|
Quote: | i'm trying to execute directly |
What exactly output do you see?
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 |
|
Back to top |
|
|
HelpdeskIVAGO
Joined: 12 Jul 2017 Posts: 10
|
Posted: Thu Sep 21, 2017 6:57 am Post subject: |
|
|
Hi Alex,
This is the output I see with the tester
Code: | [14:50:21] Script executed, correct result received:
----------
- Status: Ok
- Reply:
----------
|
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. |
|
Back to top |
|
|
KS-Soft
Joined: 03 Apr 2002 Posts: 12801 Location: USA
|
Posted: Thu Sep 21, 2017 12:52 pm Post subject: |
|
|
>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. Code: |
Set oExec = oShell.Exec(strCommand)
Do While oExec.Status = 0
WScript.Sleep 100
Loop
strResult = oExec.StdOut.ReadAll() |
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 |
|
Back to top |
|
|
HelpdeskIVAGO
Joined: 12 Jul 2017 Posts: 10
|
Posted: Fri Sep 22, 2017 2:23 am Post subject: |
|
|
I changed the script. As .vbs file it works like a charm. But when I change it from
Code: | WScript.Echo strLinet |
to
Code: | WScript.StdOut.Write statusOk & strLine |
it times-out. Even at +120 seconds. the cscript process keeps the CPU occupied untill i kill it.
Code: | 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 |
|
|
Back to top |
|
|
KS-Soft
Joined: 03 Apr 2002 Posts: 12801 Location: USA
|
Posted: Fri Sep 22, 2017 3:38 pm Post subject: |
|
|
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 |
|
Back to top |
|
|
HelpdeskIVAGO
Joined: 12 Jul 2017 Posts: 10
|
Posted: Mon Sep 25, 2017 12:37 am Post subject: |
|
|
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.
Code: | $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
} |
|
|
Back to top |
|
|
|