-
Sep 23rd, 2017, 01:55 AM
#1
Thread Starter
Registered User
[RESOLVED] Assistance with WMI Operations
Hi Everyone,
Am very much a newbie when it comes to VBScripting.
And having troubles understanding how to use/understand WMI to inspect Windows services (fist time I've had a requirement to do so in my limited scripting experience).
I'm trying to perform the following ... (please excuse my crude framework summary below)
Code:
FOR Each RUNNING Service of "iComBookingImport?" do (eg: iComBookingImport1 / iComBookingImport5 )
- Check Existence of iComBookingImport? Log file
(eg: iComBookingImport1_20171208.txt | iComBookingImport?_[ccyymmdd].txt )
+ IF NOT found - Continue to next iteration
+ IF FOUND ...
: LOOP though BookingImport? Log file to last line
: Check if last line contains string "Error: Out of memory"
: IF FALSE - COntinue to next iteration
: IF TRUE ...
= Send EMAIL to multiple Users/Distribution lists
= STOP Service "iComBookingImport?"
= Wait five(5) seconds
= START Service "iComBookingImport?"
: END-LOOP
END-FOR
Checking for the existence of files I've done before.
It's the WMI Operations that I really need assistance with if possible.
I typically run 'cscript /nologo .\[scriptname].vbs'
If there's anyone that can assist and or point me to a good source to learn how to understand vb scripting relative to WMI Operations, it would be greatly appreciated.
Thanks in advance.
Cheers,
Cameron.
-
Sep 24th, 2017, 07:16 AM
#2
Thread Starter
Registered User
Re: Assistance with WMI Operations
Hi All - Okay, I've got the following code and have confirmed different elements of this thus far with good success.
Have got to complete the STOP & START Services steps and my brain feels like spaghetti trying to see where/how I can incorporate this into the script as it is presently.
Would appreciate any direction/suggestions.
Code:
'********************************************************************************
' Script: OutOfMemory_Check.vbs [ TaskSch: Out Of Memory Check ]
' Author: Cameron Young (CY), xxx xx
' 2017.09.24 : 001. CY - 1st VB draft.
'********************************************************************************
'*DO NOT* Change strComputer value; "." represents localhost.
strComputer = "."
'Use WMI-CIMv2 object routines to interogate system services.
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
'Collect all items/instances of Windows Services that match the SQL query.
Set colListOfServices = objWMIService.ExecQuery("Select Name, State from Win32_Service where Name like 'BookingEngine[1-9]' and State = 'Running'")
'Collect ComputerSystem Name (HOSTNAME)
Set SrvrItems = objWMIService.ExecQuery("Select Name from Win32_ComputerSystem")
'Get HOSTNAME of THIS Server.
For Each srvrItem in SrvrItems
strComputerName = srvrItem.Name
Next
'Variable constants to be used.
Const ForReading = 1 'Property used by OpenTextFile operation.
Const strSearchFor = "Error: Out of memory" 'Criteria for InStr search.
Const FPath="D:\xxxxxxxxxxxx\yyyyy\zzzzzzz\" 'Folder path for the BookingEngine[1-9] log file.
'******************************************************************************
'SendEmailNotification Function
Function SendEmailNotification
Set objEmail = CreateObject("CDO.Message")
objEmail.From = strComputerName & "@local.host"
objEmail.To = "cameron.young@local.host"
objEmail.Subject = strComputerName & ": has detected an error."
objEmail.Textbody = "An """ & strSearchFor & """ condition on " & strComputerName & " has been detected with engine: " & EngineName
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.local.host"
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Send
End Function
'******************************************************************************
'GetFormattedDate Function
Function GetFormattedDate
strDate = CDate(Date)
strDay = DatePart("d", strDate)
strMonth = DatePart("m", strDate)
strYear = DatePart("yyyy", strDate)
If strDay < 10 Then
strDay = "0" & strDay
End If
If strMonth < 10 Then
strMonth = "0" & strMonth
End If
GetFormattedDate = strYear & strMonth & strDay
End Function
'******************************************************************************
'Loop through each item within {colListOfServices} and process accordingly.
For Each objItem in colListOfServices
EngineName = objItem.Name
'Assign related logfile for existance check.
FName = "ComLog_" & EngineName & "_" & GetFormattedDate & ".txt"
If (objFSO.FileExists(FPath & FName)) Then
'Open related BookingEngine logfile for inspection
Set objFile = objFSO.OpenTextFile(FPath & FName, ForReading)
'Loop Until EOF & read last line - files are relatively small (1k-200k), so should see no performance issues.
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
Loop 'Do Until objFile.AtEndOfStream
objFile.Close 'Close objFile - no further requirement.
'Assign contents of last line of logfile to variable {strSearchString}
strSearchString = strLine
'Check if {strSearchFor} string exists in {strSearchString}
If InStr(1, strSearchString, strSearchFor) > 0 then 'Ya!! FOUND strSearchFor in strSearchString
SendEmailNotification 'SEND Email Notification
'StopService() 'STOP related BookingEngine[1-9] Windows Service
WScript.Sleep(5000) 'WAIT five(5) seconds
'StartService() 'START related BookingEngine[1-9] Windows Service
End If 'InStr() operation.
End If 'objFSO.FileExists() operation.
Next 'For Each of colListOfServices
Thanks in advance.
Cheers, Cameron
-
Sep 24th, 2017, 10:44 PM
#3
Thread Starter
Registered User
Re: Assistance with WMI Operations
Update ...
The lines with 'StopService() & 'StartService() were updated to read as objItem.StopService() & objItem.StartService() .
There was a type in there too, but can't recall what it was at the present.
All worked & script works well and as expected in our DEV, TST & PROD environments.
Hadn't expected to ever use WIM/CIM, but much better for the experience.
Cheers, Cameron
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|