0

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

错误应用安全错误

这不是问题,但我厌倦了不得不反复停止我正在处理的工作以单击“继续”按钮。因此,我编写了一个程序来不断地扫描“应用安全性错误”窗口,并在找到时向其发送“Enter”键。这工作得很好,但由于中央循环永远不会终止,我决定添加在程序完成时结束程序的能力。我本可以使用 .hta 文件,但我决定尝试一种不同的方法,将所有内容保存在一个文件中,以使将来的维护更容易。我采用了这个 Stack Overflow 问题中的代码来查找我的程序的 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
4

1 回答 1

1

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

以下解决方案可能会有所帮助:提供要杀死的实例的 PID作为第二个参数(请参阅变量iPid)以及Popup...</p>

'  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
于 2020-03-19T16:33:05.420 回答