VB SCRIPT GIVING WRONG RESULTS

R

Rajdeep218

I have a Window 7 64 bit and of late I have started taking a keen interest in VB Scripts. I am appending here below a VB Script which is supposed to pull information to identify long-running processes giving the Process Name, the time duration it has been running on a specific date and time.


This is the script :


On Error Resume Next

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

' Customize start
Const processName = "MBAMService.exe"
Const maxAgeSeconds = 1
' Customize end

Dim returnCode
returnCode = 0

Set objWMIService = GetObject("winmgmts:\\localhost\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Caption='" & processName & "'", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem In colItems
Dim secAge
secAge = DateDiff("s", WMIDateStringToDate(objItem.CreationDate), Now())

If secAge > maxAgeSeconds Then
WScript.Echo "Process " & objItem.Caption & " (" & objItem.ProcessId & ") has been running for " & secAge & " seconds, since " & WMIDateStringToDate(objItem.CreationDate)

returnCode = 1
End If
Next

Function WMIDateStringToDate(dtmDate)
WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2) & "/" & _
Mid(dtmDate, 7, 2) & "/" & Left(dtmDate, 4) _
& " " & Mid (dtmDate, 9, 2) & ":" & Mid(dtmDate, 11, 2) & ":" & Mid(dtmDate,13, 2))

End Function


This script ran very well and produced a Message box as under as an example :


Process MBAMService exe. (1376) has been running for [ say 240 ] seconds since 03 Nov 19 09:30:16 AM


Between 27th Nov. 19 upto 1st Nov. 19 this script ran very well. However from 1st Nov. 19 night I noticed that this script is giving obnoxious details as under as an example:


Process MBAMService exe. (1376) has been running for [ say 222407456 ] seconds since 11 Mar 19 09:30:16 AM.


I noticed the following :

The seconds is a highly obnoxious and inflated figure, even if the system is on for 5 minutes
The current time with AM/PM suffixing is correct.
The year, 19 is correct.

The date goes back to 11 March which is wrong. Another important point is that yesterday it showed 11 Feb 19 and the day before that, it showed 11 Jan 19. Meaning that the date 11 is constant and the month keeps changing to the next successive month a day later. Tomorrow it will show 11 April 19.


I have made no changes on my Computer save except that I used a VB Script to forcefully kill wscript.exe and cscript.exe to terminate certain vb scripts which ran into a loop or kept sending never ending Message boxes eg. separate message boxes every second highlighting each process with CPU etc. The script called ' Self Destruct Script ' used was as under :

Option Explicit
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "taskkill /f /im Cscript.exe", , True

WshShell.Run "taskkill /f /im wscript.exe", , True

I failed to understand how a vb script which ran so well for a few days could pull such obnoxious details.
I would really be grateful to the community members to advise me what to do, in an effort to make this script run as intended to and as it ran earlier

Rajdeep

Continue reading...
 

Similar threads

M
Replies
0
Views
253
Michael R. Mastro II
M
Back
Top Bottom