Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.
'# D.Collins - 09:55 16/09/2010
'# Search for WScript.exe and CScript.exe processes on the local machine
Option Explicit
Dim strProc
strProc = fGetCommandLines("WScript.exe")
strProc = strProc & fGetCommandLines("CScript.exe")
MsgBox strProc,, "WSH Processes:"
Function fGetCommandLines(sPattern)
Dim oWMIService, colProcesses, strProcesses, oProcess, blTemp
Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcesses = oWMIService.ExecQuery("Select * from Win32_Process Where Name = '" & sPattern & "'")
strProcesses = ""
For Each oProcess in colProcesses
strProcesses = strProcesses & oProcess.CommandLine & vbCrlf
Next
If strProcesses = "" Then strProcesses = "No Processes with '" & sPattern & "' found."
fGetCommandLines = strProcesses
End Function
'# Put this If ... End If block at the start of your script
If InStr(1, WScript.FullName, "wscript.exe", 1) <> 0 Then
CallSelf("wscript.exe")
WScript.Quit
ElseIf InStr(1, WScript.FullName, "cscript.exe", 1) <> 0 Then
CallSelf("cscript.exe")
WScript.Quit
End If
MsgBox "Check Task Manager for '" & WScript.ScriptName & ".exe'"
'# Put this Sub at the end of your script (or anywhere you want).
Sub CallSelf(sEnv)
Dim wshShell, fso, sTmp, sWindir, sNewExe
Set wshShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
sTmp = wshShell.ExpandEnvironmentStrings("%temp%")
sWindir = wshShell.ExpandEnvironmentStrings("%WINDIR%")
sNewExe = Replace(sTmp & "\" & WScript.ScriptName & ".exe", " ", "_")
fso.CopyFile sWindir & "\System32\" & sEnv, sNewExe
wshShell.Run sNewExe & " " & WScript.ScriptFullName, 1, False
End Sub
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
Join the community of 500,000 technology professionals and ask your questions.