Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

Script that check a schedule task last run time does not work perfect.

Hi,

Script that check a schedule task last run time does not work perfect.
it does work but not for all. I think it works fine for windows 2003 Operating system machines. Not able to figure out i get results as

D559      Yes      0
D254      Yes      0
BV04      Yes      2:00:00 AM, 15/11/2009

But 70% i get 0 instead of dates.

regards
sharath
On Error Resume Next
Set objShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile("\\dsm\Logs\ScheduleTask.txt", 8, TRUE) 
Set objExecObject = objShell.Exec("cmd /c hostname")
strComputerName = objExecObject.StdOut.ReadLine
Set objExecObject = objShell.Exec("cmd /c schtasks -query -fo csv -v | findstr ""Infected Scan""")
strText = objExecObject.StdOut.ReadAll
If strText = "" Then
	objFile.WriteLine strComputerName & vbTab & "No"
Else
	auxArr = Split(strText, """,""")
	objFile.WriteLine strComputerName & vbTab & "Yes" & vbTab & auxarr(5)
End If

Open in new window

Avatar of Netcraft
Netcraft
Flag of Netherlands image

I suspect that the Split doesn't work as expected.
Can you post the output you get when you insert between line 12 and 13:
      objFile.WriteLine strComputerName & vbTab strText

Maybe there's an extra "," in between...
Avatar of bsharath

ASKER

Sorry could not follow. Can you give me the full code...
Any views...
Avatar of Chris Bottomley
Referring to netcrafts comment can you supply the debug trace for the run using the following code adaptation.

Chris
On Error Resume Next
Set objShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile("\\dsm\Logs\ScheduleTask.txt", 8, TRUE) 
Set objExecObject = objShell.Exec("cmd /c hostname")
strComputerName = objExecObject.StdOut.ReadLine
Set objExecObject = objShell.Exec("cmd /c schtasks -query -fo csv -v | findstr ""Infected Scan""")
strText = objExecObject.StdOut.ReadAll
debug.print ">>>" & strtext & "<<<"
If strText = "" Then
	objFile.WriteLine strComputerName & vbTab & "No"
Else
	auxArr = Split(strText, """,""")
	objFile.WriteLine strComputerName & vbTab & "Yes" & vbTab & auxarr(5)
End If

Open in new window

Thanks but still get 0 for some machines.
I think its not working for XP and works for Windows 2003 & Vista
I haven't changed the function ... The idea is you supply the line(s) printed to the immediate window in teh VBE for analysis.

Chris
Is this a Excel macro...I was trying as vbs
Any ways i placed it in excel and ran it i got this lines only

>>><<<
DOes this happen with entries that work like BV04 or the ones that fail?

Chris
Sorry could not follow
Script that check a schedule task last run time does not work perfect.
it does work but not for all. I think it works fine for windows 2003 Operating system machines. Not able to figure out i get results as

D559      Yes      0
D254      Yes      0
BV04      Yes      2:00:00 AM, 15/11/2009

But 70% i get 0 instead of dates.

Run the modified sub to achieve the exact same results .... then goto the immediate window and copy the displayed data there ... presumably three lines of test one for each of D559/D254/BV04

Chris
Chris do you mean run this code
 ID: 25859243
In an excel  Module then place the machine name in the immediate window.?
Sort of.  Yes run that code instead of the original and as a result of running it an output should appear in the immediate window.

Chris
I get this

---------------------------
Microsoft Visual Basic
---------------------------
Compile error:

Sub or Function not defined
---------------------------
OK   Help  
---------------------------
Ah! ... THe original was a VBS so try this instead for a message box with the output which you'll need to note.

Chris
On Error Resume Next
Set objShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile("\\dsm\Logs\ScheduleTask.txt", 8, TRUE) 
Set objExecObject = objShell.Exec("cmd /c hostname")
strComputerName = objExecObject.StdOut.ReadLine
Set objExecObject = objShell.Exec("cmd /c schtasks -query -fo csv -v | findstr ""Infected Scan""")
strText = objExecObject.StdOut.ReadAll
msgbox ">>>" & strtext & "<<<"
If strText = "" Then
	objFile.WriteLine strComputerName & vbTab & "No"
Else
	auxArr = Split(strText, """,""")
	objFile.WriteLine strComputerName & vbTab & "Yes" & vbTab & auxarr(5)
End If

Open in new window

I get this vista

---------------------------

---------------------------
>>>"DE06","\Microsoft\Windows Defender\MP Scheduled Scan","11/20/2009 1:55:00 AM","Ready","Interactive/Background","11/19/2009 1:54:59 AM","0","N/A","c:\program files\windows defender\MpCmdRun.exe Scan -RestrictPrivileges","N/A","Scheduled Scan","Enabled","Disabled","No Start On Batteries","SYSTEM","Enabled","72:00:00","Scheduling data is not available in this format.","Daily ","1:55:00 AM","1/1/2000","1/1/2100","Every 1 day(s)","N/A","Disabled","Disabled","Disabled","Disabled"

<<<
---------------------------
OK  
---------------------------

Windows 2003

---------------------------

---------------------------
>>>"PHOS","Infected Scan","2:00:00 AM, 11/20/2009","","Background only","Never","0","SYSTEM","At 2:00 AM every Mon, Tue, Wed, Thu, Fri, Sat, Sun of every week, starting 11/19/2009","C:\Program Files\Sophos\Sophos Anti-Virus\BackgroundScanClient.exe {BC5638AB-0830-42EA-92BF-96CD2191F98F}","N/A","N/A","Enabled","Weekly","2:00:00 AM","11/19/2009","N/A","SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY","N/A","NT AUTHORITY\SYSTEM","Disabled","72:0","Disabled","Disabled","Disabled","Disabled","Disabled","No Start On Batteries, Stop On Battery Mode"

<<<
---------------------------
OK  
---------------------------

Windows XP

---------------------------

---------------------------
>>>"DEV41","Infected Scan","02:00:00, 11/20/2009","","Never","0","SYSTEM","At 2:00 AM every Mon, Tue, Wed, Thu, Fri, Sat, Sun of every week, starting 11/19/2009","C:\Program Files\Sophos\Sophos Anti-Virus\BackgroundScanClient.exe {7F625AA5-B31F-4EE3-BAB3-7A572CFFEF7A}","N/A","N/A","Enabled","Weekly","02:00:00","11/19/2009","N/A","SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY","N/A","NT AUTHORITY\SYSTEM","Disabled","72:0","Disabled","Disabled","Disabled","Disabled","Disabled","Disabled"

"DEV41","MP Scheduled Scan","01:54:00, 11/20/2009","","Never","0","SYSTEM","At 1:54 AM every day, starting 11/19/2009","C:\Program Files\Windows Defender\MpCmdRun.exe Scan -RestrictPrivileges","N/A","Scheduled Scan","Enabled","Daily ","01:54:00","11/19/2009","N/A","Everyday","N/A","NT AUTHORITY\SYSTEM","Disabled","72:0","Disabled","Disabled","Disabled","Disabled","Disabled","No Start On Batteries"

<<<
---------------------------
OK  
---------------------------
Array element 5 seems to be different across the OS types would be my immediate comment!  Personally i'm not adept enough to take it further without a lot of research as it's outside my skills base.

Chris
Try this as a test.

Post the msgbox contents as you did before, and check the log file to see if the results are correct (verify correct)

This will tell me a few things and how to progress towards solution.
On Error Resume Next
Set objShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile("\\dsm\Logs\ScheduleTask.txt", 8, TRUE) 
Set objExecObject = objShell.Exec("cmd /c hostname")
strComputerName = objExecObject.StdOut.ReadLine
Set objExecObject = objShell.Exec("cmd /c schtasks -query -fo csv -v | findstr ""Infected Scan""")
strText = objExecObject.StdOut.ReadAll
msgbox ">>>" & strtext & "<<<"
If strText = "" Then
	objFile.WriteLine strComputerName & vbTab & "No"
Else
	auxArr = Split(strText, ",")
	objFile.WriteLine strComputerName & vbTab & "Yes" & vbTab & auxarr(3)
End If

Open in new window

Hmm, i think I added one too many.

Change

objFile.WriteLine strComputerName & vbTab & "Yes" & vbTab & auxarr(3)

To

objFile.WriteLine strComputerName & vbTab & "Yes" & vbTab & auxarr(2)

I expect the the XP machine will not be correct, but I will need to know for sure.
I get this

---------------------------

---------------------------
>>>"IN-01","Infected Scan","2:00:00 AM, 11/26/2009","","Background only","2:00:00 AM, 11/25/2009","0","SYSTEM","At 2:00 AM every Mon, Tue, Wed, Thu, Fri, Sat, Sun of every week, starting 11/22/2009","C:\Program Files\Sophos\Sophos Anti-Virus\BackgroundScanClient.exe {63A0F059-D93F-4F12-9ECC-01F732084330}","N/A","N/A","Enabled","Weekly","2:00:00 AM","11/22/2009","N/A","SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY","N/A","NT AUTHORITY\SYSTEM","Disabled","72:0","Disabled","Disabled","Disabled","Disabled","Disabled","Disabled"

<<<
---------------------------
OK  
---------------------------
For info since as I added earlier I am not aware of SCHTASKS usage and some quick research failed to provide usage information.

I have just plugged the data into a script to analyse and get:

VISTA ELement 5 = "11/19/2009 1:54:59 AM"
2K3 ELement 5 = "Background only"
XP ELement 5 = "Never"

This confirms to me that the returned responses across the operating systems is different since if you expect a date in each instance then in two you are getting a string and thereforer you will need to test the OS to establish which parameter to return.  

Chris
It would seem that the response from the XP that returns "Never".  Have you verified that on a machine that returns a Negative that it has actually ran before?  From all the posts from you with the long strings, the entries that have a "0" in the date returned are the ones that says it "never" has ran.

If it is a 2k3 then should be arr(6) instead of (5)
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank U Rob...