Vbscript 从wscript.exe进程获取脚本名称
我正在使用以下代码:Vbscript 从wscript.exe进程获取脚本名称,vbscript,Vbscript,我正在使用以下代码: Dim name name = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%computername%") Set wmi = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & name & "\root\cimv2") For Each hwnd In wmi.Instance
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
字符串以查找所有运行脚本的路径和名称。您有ScriptName
和ScriptFullName
属性
' in VBScript
WScript.Echo WScript.ScriptName
WScript.Echo WScript.ScriptFullName
// in JScript
WScript.Echo(WScript.ScriptName);
WScript.Echo(WScript.ScriptFullName);
[编辑]在这里(使用.CommandLine
属性):
也许我的问题令人困惑。我知道如何获取当前运行的vbscript的名称。我在问如何从另一个wscript.exe.Aha的句柄中获取正在运行的脚本的名称,这是我的错,现在我明白你的要求了。好的,我将编辑我的答案;-)
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
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