Ways to monitor windows scheduled tasks

All questions related to installations, configurations and maintenance of Advanced Host Monitor (including additional tools such as RMA for Windows, RMA Manager, Web Servie, RCC).
Ryan
Posts: 7
Joined: Sun Dec 10, 2006 9:28 pm

Ways to monitor windows scheduled tasks

Post by Ryan »

Is there a way for KS-Soft to monitor Windows Scheduled Tasks?


Many Thanks,
Ryan
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

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
Ryan
Posts: 7
Joined: Sun Dec 10, 2006 9:28 pm

Post by Ryan »

Thanks for prompt reply, :D

But the WMI only works for Jobs created using AT command, how about jobs created with the Windows Scheduled Task?

Many Thanks,
Ryan
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

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/mfra ... m#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
Ryan
Posts: 7
Joined: Sun Dec 10, 2006 9:28 pm

Post by Ryan »

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. :cry:
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

Ryan 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. :cry:
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=2621

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
HelpdeskIVAGO
Posts: 10
Joined: Wed Jul 12, 2017 7:56 am

Post by HelpdeskIVAGO »

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"
KS-Soft
Posts: 13012
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

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
HelpdeskIVAGO
Posts: 10
Joined: Wed Jul 12, 2017 7:56 am

Post by HelpdeskIVAGO »

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: 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 
My goal is to see if the scheduled task ran succesfully
KS-Soft
Posts: 13012
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

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
HelpdeskIVAGO
Posts: 10
Joined: Wed Jul 12, 2017 7:56 am

Post by HelpdeskIVAGO »

Hi Alex,

This is the output I see with the tester

Code: Select all

[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.
KS-Soft
Posts: 13012
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

>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: Select all

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
HelpdeskIVAGO
Posts: 10
Joined: Wed Jul 12, 2017 7:56 am

Post by HelpdeskIVAGO »

I changed the script. As .vbs file it works like a charm. But when I change it from

Code: Select all

WScript.Echo strLinet
to

Code: Select all

WScript.StdOut.Write statusOk  & strLine
it times-out. Even at +120 seconds. the cscript process keeps the CPU occupied untill i kill it.

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
KS-Soft
Posts: 13012
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

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
HelpdeskIVAGO
Posts: 10
Joined: Wed Jul 12, 2017 7:56 am

Post by HelpdeskIVAGO »

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: 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
}
Post Reply