Dec 21

ListRunningScripts.vbs

'=========================================================================
' ListRunningScripts.vbs
' VERSION: 1.1 - Null value is returned when a script is launched by another user on the local computer and the script is not elevated for UAC. I added a check to look for this and alert the user.
' AUTHOR: Brian Steinmeyer
' EMAIL: sigkill@sigkillit.com
' WEB: http://sigkillit.com
' DATE: 12/21/2012
' COMPATIBLE: Windows XP, Server 2003, and Above
' COMMENTS: If you ever had a wscript.exe or cscript.exe process running but
' need to know the exact script it is running you can find the answer using
' the CommandLine property in the WIN32_Process Class. You can view this
' property in Task Manager by clicking View->Choose Columns. Alternatively,
' you can script it as I have below. The script works on the local computer
' as well as remote computers. Set the computer to run the script against
' and optionally set it to show the Scripting Host that launched the script.
' EXAMPLE: List Running Scripts on Local Computer Without Script Host
'          Dim strComputer: strComputer = "."
'          Dim blnShowScriptHost: blnShowScriptHost = False
' EXAMPLE: List Running Scripts on Local Computer With Script Host
'          Dim strComputer: strComputer = "."
'          Dim blnShowScriptHost: blnShowScriptHost = True
' EXAMPLE: List Running Scripts on Remote Computer Without Script Host
'          Dim strComputer: strComputer = "server"
'          Dim blnShowScriptHost: blnShowScriptHost = False
' EXAMPLE: List Running Scripts on Remote Computer With Script Host
'          Dim strComputer: strComputer = "server"
'          Dim blnShowScriptHost: blnShowScriptHost = True
'=========================================================================
Option Explicit
' ------ SCRIPT CONFIGURATION ------
Dim strComputer: strComputer = "."
Dim blnShowScriptHost: blnShowScriptHost = False
' ------ END CONFIGURATION ------

Wscript.Echo ListRunningScripts(strComputer)

Private Function ListRunningScripts(strComputer)

    On Error Resume Next

    Dim objWMIService: Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Dim colProcesses: Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = " & "'Wscript.exe' OR Name = 'Cscript.exe'")
    Dim objProcess
    Dim strResult: strResult = ""
    Dim count: count = 0
    If Not colProcesses.Count = 0 Then
        For Each objProcess in colProcesses
            count = count + 1
            If blnShowScriptHost = True Then
                strResult = strResult & objProcess.CommandLine & vbCrLf
            Else
                If IsNull(objProcess.CommandLine) OR IsNull(objProcess.ExecutablePath) Then
                    strResult = strResult & "NULL (You need to use Run As Administrator)" & vbCrLf
                Else
                    strResult = strResult & Trim(Replace(Replace(objProcess.CommandLine, objProcess.ExecutablePath, "", 1, 1, 1), objProcess.Name, 1, 1, 1)) & vbCrLf
                End If
            End If
        Next
    End If

    strResult = count & " Scripts are running" & vbCrLf & "----------------------" & vbCrLf & strResult

    If Err.Number <> 0 Then
        Err.Clear
        strResult = "!~ERROR~!"
    End If

    ListRunningScripts = strResult

    On Error Goto 0

End Function