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.vbsjscript.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 に答える