从wscript.exe进程获取脚本名称

Bob*_*Bob 2 vbscript

我正在使用此代码:

Dim name
name = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%computername%")
Set wmi = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _ 
    & name & "\root\cimv2")
For Each hwnd In wmi.InstancesOf("Win32_Process")
    If hwnd.Name = "wscript.exe" Then
        'get name and possibly location of currently running script
    End If
Next
Run Code Online (Sandbox Code Playgroud)

我成功列出了所有流程并选择了wscript.exe.但是,我搜索过,发现没有办法找到运行的脚本的名称wscript.exe,即.是myscript.vbsjscript.js什么的.如果有办法找到脚本的整个路径,可以获得奖励.

编辑:

通过更多搜索,我找到了解决方案.在上面的脚本中,hwnd变量存储wscript.exe进程的句柄.手柄有一个属性:hwnd.CommandLine.它显示了如何从命令行调用它,所以它将是这样的:

"C:\Windows\System32\wscript.exe" "C:\path\to\script.vbs"
Run Code Online (Sandbox Code Playgroud)

所以我可以解析hwnd.CommandLine字符串来查找所有正在运行的脚本的路径和名称.

Pan*_*lov 10

你有ScriptNameScriptFullName属性.

' in VBScript
WScript.Echo WScript.ScriptName
WScript.Echo WScript.ScriptFullName

// in JScript
WScript.Echo(WScript.ScriptName);
WScript.Echo(WScript.ScriptFullName);
Run Code Online (Sandbox Code Playgroud)

[编辑]你去(使用.CommandLine财产):

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

Set colProcesses = objWMIService.ExecQuery( _
    "Select * from Win32_Process " _
    & "Where Name = 'WScript.exe'", , 48)

Dim strReport
For Each objProcess in colProcesses
    ' skip current script, and display the rest
    If InStr (objProcess.CommandLine, WScript.ScriptName) = 0 Then
        strReport = strReport & vbNewLine & vbNewLine & _
            "ProcessId: " & objProcess.ProcessId & vbNewLine & _
            "ParentProcessId: " & objProcess.ParentProcessId & _
            vbNewLine & "CommandLine: " & objProcess.CommandLine & _
            vbNewLine & "Caption: " & objProcess.Caption & _
            vbNewLine & "ExecutablePath: " & objProcess.ExecutablePath
    End If
Next
WScript.Echo strReport
Run Code Online (Sandbox Code Playgroud)