VBScript 程序错误地识别了它的 PID?

VBScript program incorrectly identifying its PID?

每次我需要授予用户对我们服务器上的文件共享的访问权限时,我都会多次出现此弹出窗口,因为该过程会遇到我无法修改其访问权限的各种文件:

这不是问题,但我厌倦了不得不反复停止正在处理的工作以单击 "Continue" 按钮。因此,我编写了一个程序来不断扫描 "Error Applying Security" window,只要找到一个,就向它发送 "Enter" 按键。这工作得很好,但由于中央循环永远不会终止,我决定添加在程序完成时结束程序的功能。我本可以使用 .hta 文件,但我决定尝试一种不同的方法,将所有内容保存在一个文件中,以便将来维护更容易。我采用了 this Stack Overflow question 中的代码来查找我的程序的 PID,并允许在关闭弹出窗口后使用 Windows TASKKILL 命令结束程序。

该程序似乎可以正常运行,识别其 PID 并将其传递给弹出窗口。但是,当 TASKKILL 运行时,它声称 PID 不存在。谁能看到我做错了什么?预先感谢所有回复的人。

'  AutoContinue.vbs
'  Program to automatically close all "Error Applying Security" messages

Option Explicit

Const Hidden = 0
Dim objWshShell, objWMILocator, objWMIService
Dim strComputerName, objArgs, objChildProcess, colPIDs, objPID

Set objWshShell = CreateObject( "WScript.Shell" )
Set objWMILocator = CreateObject( "WBemScripting.SWbemLocator" )
Set objWMIService = objWMILocator.ConnectServer( "", "", "", "" )

Function MyProcessID ()
' MyProcessID finds and returns my own PID.

    MyProcessID = 0
    Set objChildProcess = objWshShell.Exec( "%comspec% /C pause" )
    Set colPIDs= objWMIService.ExecQuery( "Select * From Win32_Process" & _
        " Where ProcessId=" & objChildProcess.ProcessID,, 0 )
    For Each objPID In colPIDs
        MyProcessID = objPID.ParentProcessID
    Next
    Call objChildProcess.Terminate()
End Function

Set objArgs = WScript.Arguments
If objArgs.Count = 1 Then
    If objArgs.Item(0) = "Popup" Then
        objWshShell.Popup( "AutoContinue PID is " & MyProcessID & _
            vbCrLf & "Hit OK when done." )
'        objWshShell.Run "taskkill /PID " & MyProcessID & " /T", 1
        objWshShell.Run "%comspec% /k taskkill /PID " & MyProcessID & " /T", 1
        Set objArgs = Nothing
        Set objWshShell = Nothing
        WScript.Quit
    End If
End If

objWshShell.Run "wscript.exe " & WScript.ScriptName & " Popup", Hidden

Do
    Do
        WScript.Sleep( 500 )
    Loop While Not objWshShell.AppActivate( "Error Applying Security" )
    WScript.Sleep( 100 )
    objWshShell.AppActivate( "Error Applying Security" )
    WScript.Sleep( 100 )
    objWshShell.SendKeys( "{ENTER}" )
Loop While True

Set objWshShell = Nothing

脚本正确识别了自己的 PID。但是,您试图杀死自己(带有提供的 Popup 参数的脚本实例),而您需要在没有它(或使用另一个第一个参数?)的情况下杀死脚本实例。

以下解决方案可能有所帮助:提供 要杀死的实例的 PID 作为第二个参数(参见变量 iPid)以及 Popup 一个……

'  AutoContinue.vbs
'  Program to automatically close all "Error Applying Security" messages
Option Explicit
Const Hidden = 0
Dim objWshShell, objWMILocator, objWMIService
Dim strComputerName, objArgs, objChildProcess, colPIDs, objPID

Set objWshShell = CreateObject( "WScript.Shell" )
Set objWMILocator = CreateObject( "WBemScripting.SWbemLocator" )
Set objWMIService = objWMILocator.ConnectServer( "", "", "", "" )

Function MyProcessID ()
' MyProcessID finds and returns my own PID.

    MyProcessID = 0
    Set objChildProcess = objWshShell.Exec( "%comspec% /C pause" )
    Set colPIDs= objWMIService.ExecQuery( "Select * From Win32_Process" & _
        " Where ProcessId=" & objChildProcess.ProcessID,, 0 )
    For Each objPID In colPIDs
        MyProcessID = objPID.ParentProcessID
    Next
    Call objChildProcess.Terminate()
End Function

Dim iPid
iPid = MyProcessID()

Set objArgs = WScript.Arguments
If objArgs.Count >= 2 Then
    If objArgs.Item(0) = "Popup" Then
        objWshShell.Popup( "AutoContinue PID is " & objArgs.Item(1) & _
            vbCrLf & "Hit OK when done." )
'        objWshShell.Run "taskkill /PID " & objArgs.Item(1) & " /T /F", 1
        objWshShell.Run "%comspec% /k taskkill /PID " & objArgs.Item(1) & " /T /F", 1
        Set objArgs = Nothing
        Set objWshShell = Nothing
        WScript.Quit
    End If
End If

objWshShell.Run "wscript.exe """ _
    & WScript.ScriptFullName & """ Popup " & CStr( iPid) , Hidden
''                                 ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ two arguments

Do
     Do
        WScript.Sleep( 500 )
     Loop While Not objWshShell.AppActivate( "Error Applying Security" )
     WScript.Sleep( 100 )
     objWshShell.AppActivate( "Error Applying Security" )
     WScript.Sleep( 100 )
     objWshShell.SendKeys( "{ENTER}" )
Loop While True