gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 18:58:14 24 4
gpt4 key购买 nike

我正在使用此代码:

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 字符串来查找所有正在运行的脚本的路径和名称。

最佳答案

您有 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

关于vbscript - 从 wscript.exe 进程获取脚本名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15125480/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com