2

我正在使用这段代码:

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

我成功地列出了所有进程并选择了wscript.exe. 但是,我已经搜索并没有找到在 中运行的脚本的名称wscript.exe,即。是它myscript.vbs还是jscript.js什么。如果有办法找到脚本的整个路径,则可以加分。

编辑:

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

"C:\Windows\System32\wscript.exe" "C:\path\to\script.vbs"

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

4

2 回答 2

10

你有ScriptNameScriptFullName属性。

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

// in JScript
WScript.Echo(WScript.ScriptName);
WScript.Echo(WScript.ScriptFullName);

[编辑] 给你(使用.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
于 2013-02-28T07:19:31.257 回答
0
myProcess="wscript.exe"               
Set Processes = GetObject("winmgmts:").InstancesOf("Win32_Process")
For Each Process In Processes
 If StrComp(Process.Name, myProcess, vbTextCompare) = 0 Then     'check if process exist      
   CmdLine=process.commandline                  
 End If
Next
myArr=split(CmdLine,"\")
mySN=replace(myArr(ubound(myArr)),"""","")
Wscript.Echo "Full Pth: " & Cmdline &vbcrlf&"Script Name: "& mySN  
于 2018-10-10T05:56:24.887 回答