4

プロセスが特定のユーザーによって使用されているかどうかを確認する VBScript が必要です。

エージェントがプログラム アイコンをクリック --> progcheck.vbs のバッチ ファイル呼び出し -->

progcheck.vbs は、"whatever.exe" がそのユーザーのみで実行されていることを確認します -->

プログラムがそのユーザーの下で実行されている場合、MsgBox "Program running" --> wscript.quit (これはバッチ ファイルから終了する必要があります)

そうでなければ --> バッチ ファイルに戻ります。

バッチ ファイルの tasklist でこれを試してみましたが、スクリプトは機能しますが、ドメイン ユーザーに対して実行するのに時間がかかります。とにかくvbscriptでこれをやりたい。

***MOD 10/12 で更新されたスクリプト** * **

OPTION EXPLICIT

    DIM strComputer,strProcess, strUserName,wshShell

    Set wshShell = WScript.CreateObject( "WScript.Shell" )
    strUserName = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )

    strComputer = "." ' 
    strProcess = "notepad.exe"


    IF isProcessRunning(strComputer,strProcess,strUserName) THEN
            If MsgBox ("Notepad needs to be closed.", 1) = 1 then
                wscript.Quit(1)
            End If
    END IF

FUNCTION isProcessRunning(BYVAL strComputer,BYVAL strProcessName,BYVAL strUserName)

    DIM objWMIService, strWMIQuery

    strWMIQuery = "Select * from Win32_Process where name like '" & strProcessName & "' AND owner like '" &strUserName& "'" 

    SET objWMIService = GETOBJECT("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" _ 
            & strComputer & "\root\cimv2") 


    IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
        isProcessRunning = TRUE
    ELSE
        isProcessRunning = FALSE
    END If

End Function

あなたの考えと、どこが間違っているか教えてください。前もって感謝します。

4

2 に答える 2

7

更新されたコード v3:ヘルプのコメントを確認してください

OPTION EXPLICIT

    DIM strComputer, strProcess, strUserName, wshShell

    Set wshShell = WScript.CreateObject( "WScript.Shell" )
    strUserName = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )
    strComputer = "."
    strProcess = "notepad.exe" 'change this to whatever you are trying to detect

    IF isProcessRunning(strComputer, strProcess, strUserName) THEN
        If MsgBox ("Notepad needs to be closed.", 1) = 1 then
            wscript.Quit(1) 'you need to terminate the process if that's your intention before quitting
        End If
    Else
        msgbox ("Process is not running") 'optional for debug, you can remove this
    END IF

FUNCTION isProcessRunning(ByRef strComputer, ByRef strProcess, ByRef strUserName)

    DIM objWMIService, strWMIQuery, objProcess, strOwner, Response

    strWMIQuery = "SELECT * FROM Win32_Process WHERE NAME = '" & strProcess & "'"

    SET objWMIService = GETOBJECT("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2").ExecQuery(strWMIQuery)

    IF objWMIService.Count > 0 THEN
        msgbox "We have at least ONE instance of Notepad"
        For Each objProcess in objWMIService
            Response = objProcess.GetOwner(strOwner)
            If Response <> 0 Then
                'we didn't get any owner information - maybe not permitted by current user to ask for it
                Wscript.Echo "Could not get owner info for process [" & objProcess.Name & "]" & VBNewLine & "Error: " & Return
            Else 
                Wscript.Echo "Process [" & objProcess.Name & "] is owned by [" & strOwner & "]" 'for debug you can remove it
                if strUserName = strOwner Then
                    msgbox "we have the user who is running notepad"
                    isProcessRunning = TRUE
                Else
                    'do nothing as you only want to detect the current user running it
                    isProcessRunning = FALSE
                End If
            End If
        Next
    ELSE
        msgbox "We have NO instance of Notepad - Username is Irrelevant"
        isProcessRunning = FALSE
    END If

End Function

次の機能を使用できます。

FUNCTION isProcessRunning(BYVAL strComputer,BYVAL strProcessName)

    DIM objWMIService, strWMIQuery

    strWMIQuery = "Select * from Win32_Process where name like '" & strProcessName & "'"

    SET objWMIService = GETOBJECT("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" _ 
            & strComputer & "\root\cimv2") 


    IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
        isProcessRunning = TRUE
    ELSE
        isProcessRunning = FALSE
    END IF

END FUNCTION

使用するローカルコンピューターの場合"."

プロセス名には、実行可能ファイルを使用します"notepad.exe"

コードの残りの部分については、単純なものを使用できます。

OPTION EXPLICIT
DIM strComputer,strProcess

strComputer = "." ' local computer
strProcess = "notepad.exe" 'whatever is the executable

IF isProcessRunning(strComputer,strProcess) THEN
    'do something
ELSE
    'do something else or nothing
    wscript.echo strProcess & " is NOT running on computer '" & strComputer & "'"
END IF

それはそれを行う必要があります。

追加

実行中のすべてのプロセスを表示するには、次を実行します。

Option Explicit
Dim objWMIService, objProcess, colProcess
Dim strComputer, strList

strComputer = "."

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

Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process")

For Each objProcess in colProcess
    strList = strList & vbCr & _
    objProcess.Name
Next

WSCript.Echo strList
WScript.Quit
于 2013-10-12T02:38:49.250 に答える