Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB script that parses a list of processes and checks if they're runnin 1

Status
Not open for further replies.

apdesai

MIS
Aug 18, 2009
4
US
I need to parse a list of processes in a text file to run the check, and spit out a non zero exit code if any of the processes listed in the text file are running. Can be a csv file too, though am assuming a .ini with a process listed on each line would be easiest. Any help is greatly appreciated! I have zero VB scripting experience, but the boss doesn't care about that!

I've got this script that checks for a hard coded process, assuming I can build off this...


' List the processes Running on a Computer



strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colProcesses = objWMIService.ExecQuery _
("SELECT * FROM Win32_Process WHERE Name = " & _
"'notepad.exe'")

If colProcesses.Count = 0 Then
Wscript.Echo "No scripts are running."
Else
For Each objProcess in colProcesses
Wscript.Echo objProcess.CommandLine
Next
End If
 
So I've gotten this far, still having an issue with line 68. Any thoughts?!?

(68, 5) SWbemObjectSet: Invalid query

I've verified that it is parsing the lines from a file into an array, its just around the WMI query and the count statement. Any help would be greatly appreciated! Thanks! :)


'*************************
'** Error Checking Routine
'*************************
Sub ThrowError (Number, Description)
ObjApp = ""
WScript.Echo WScript.ScriptName + " " + CStr(number) + " " + description
NwmScript.Log WScript.ScriptName + " " + CStr(number) + " " + description
WScript.Quit number
End Sub

Sub EchoAndLog ( message )
WScript.Echo message
NwmScript.Log WScript.ScriptName + " - " + message
End Sub

' Parse processes list into array

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\temp\processes.csv", ForReading)

Const ForReading = 1

Dim arrFileLines()
i = 0
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
Loop
objFile.Close


' List the processes Running on a Computer
For Each strLine in arrFileLines

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colProcesses = objWMIService.ExecQuery _
("SELECT * FROM Win32_Process WHERE Name =" & strLine &"")
If colProcesses.Count=0 Then
Wscript.Echo "No scripts are running."
End If

Next
 
This should get you in the right direction...

Code:
Set colProcesses = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_Process WHERE Name = [COLOR=blue yellow]'[/color]" & strLine & "[COLOR=blue yellow]'[/color]")
    If colProcesses.Count=0 Then
    Wscript.Echo [COLOR=blue yellow]strLine & " is not[/color] running."
 End If

BTW, "line 68" is not very descriptive, especially since you did not post 68 lines of code! Besides posting your code, you should also figure out which line is line 68, and post it.
 
apologies...Left out some non essential bits of the script. The line it keeps failing on is...

If colProcesses.Count=0 Then

 
You could try something like this

Code:
Option Explicit

Main()

Sub Main()
	Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
	Dim arrProcesses : arrProcesses = Split(objFSO.OpenTextFile("C:\Temp\Processes.txt").ReadAll, VbCrLf)
	Dim objWMIService : Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
	Dim colProcesses : Set colProcesses = objWMIService.ExecQuery(_
										  "SELECT Name FROM Win32_Process WHERE Name = '" & Join(arrProcesses, "' OR Name = '") & "'")
	If colProcesses.Count > 0 Then
		WScript.Echo "One or more of the listed processes detected"
	Else
		WScript.Echo "NO processes listed detected"
	End If
End Sub

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
This solved the problem. I knew it was a syntax issue. Thanks much guitarzan, and also dm4ever, and anyone else that took the time to look at this and help a noob out...

Set colProcesses = objWMIService.ExecQuery _
("SELECT * FROM Win32_Process WHERE Name = '" & strLine & "'")
If colProcesses.Count=0 Then
Wscript.Echo strLine & " is not running."
End If

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top