KS-Soft. Network Management Solutions
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister    ProfileProfile    Log inLog in 

Ways to monitor windows scheduled tasks
Goto page 1, 2  Next
 
Post new topic   Reply to topic    KS-Soft Forum Index -> Configuration, Maintenance, Troubleshooting
View previous topic :: View next topic  
Author Message
Ryan



Joined: 10 Dec 2006
Posts: 7

PostPosted: Thu Jun 14, 2007 10:15 pm    Post subject: Ways to monitor windows scheduled tasks Reply with quote

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


Many Thanks,
Ryan
Back to top
View user's profile Send private message
KS-Soft Europe



Joined: 16 May 2006
Posts: 2833

PostPosted: Fri Jun 15, 2007 12:21 am    Post subject: Reply with quote

I think, you may use "WMI" test method: http://www.ks-soft.net/hostmon.eng/mframe.htm#tests.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
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Ryan



Joined: 10 Dec 2006
Posts: 7

PostPosted: Fri Jun 15, 2007 3:48 am    Post subject: Reply with quote

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
View user's profile Send private message
KS-Soft Europe



Joined: 16 May 2006
Posts: 2833

PostPosted: Fri Jun 15, 2007 4:21 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
Ryan



Joined: 10 Dec 2006
Posts: 7

PostPosted: Fri Jun 15, 2007 4:32 am    Post subject: Reply with quote

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
View user's profile Send private message
KS-Soft Europe



Joined: 16 May 2006
Posts: 2833

PostPosted: Fri Jun 15, 2007 4:53 am    Post subject: Reply with quote

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.
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/ArticleID/25186/25186.html
http://makemsi-manual.dennisbareis.com/schtasks_exe.htm
http://home.aol.com/bergert/w2k/tip23.htm

Regards,
Max
Back to top
View user's profile Send private message Send e-mail Visit poster's website
HelpdeskIVAGO



Joined: 12 Jul 2017
Posts: 9

PostPosted: Thu Sep 21, 2017 2:02 am    Post subject: Reply with quote

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
View user's profile Send private message
KS-Soft



Joined: 03 Apr 2002
Posts: 11774
Location: USA

PostPosted: Thu Sep 21, 2017 5:01 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
HelpdeskIVAGO



Joined: 12 Jul 2017
Posts: 9

PostPosted: Thu Sep 21, 2017 5:31 am    Post subject: Reply with quote

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
View user's profile Send private message
KS-Soft



Joined: 03 Apr 2002
Posts: 11774
Location: USA

PostPosted: Thu Sep 21, 2017 6:45 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
HelpdeskIVAGO



Joined: 12 Jul 2017
Posts: 9

PostPosted: Thu Sep 21, 2017 6:57 am    Post subject: Reply with quote

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
View user's profile Send private message
KS-Soft



Joined: 03 Apr 2002
Posts: 11774
Location: USA

PostPosted: Thu Sep 21, 2017 12:52 pm    Post subject: Reply with quote

>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
View user's profile Send private message Send e-mail Visit poster's website
HelpdeskIVAGO



Joined: 12 Jul 2017
Posts: 9

PostPosted: Fri Sep 22, 2017 2:23 am    Post subject: Reply with quote

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
View user's profile Send private message
KS-Soft



Joined: 03 Apr 2002
Posts: 11774
Location: USA

PostPosted: Fri Sep 22, 2017 3:38 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
HelpdeskIVAGO



Joined: 12 Jul 2017
Posts: 9

PostPosted: Mon Sep 25, 2017 12:37 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    KS-Soft Forum Index -> Configuration, Maintenance, Troubleshooting All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group

KS-Soft Forum Index