gpt4 book ai didi

VBScript 的 ShellExecute 返回代码

转载 作者:行者123 更新时间:2023-12-02 21:55:31 26 4
gpt4 key购买 nike

是否有可能通过返回码(可能是 Bool?)来了解 UAC 对话框是否被接受或拒绝?我现在不做检查就这样做了,如下。

Set Shell = CreateObject("Shell.Application")
Shell.ShellExecute "wscript.exe", """" & SOME_LOG_PARSING_SCRIPT_FULLPATH_HERE & """ uac", "", "runas"

如果我能知道用户是否接受了 uac 对话框(如果有),那就太好了。

最佳答案

数十次说过: basically, no, what you're asking isn't possible. 但是,有一种解决方法可以以高可能性回答问题,尽管仍然存在错误响应的非零概率。这是此类解决方案的草稿,请参阅下面的脚本:

  • 如果已经提升,则答案为 100%:不需要 UAC 提示
  • 否则,在 UAC 提示符之前和之后对 提升 wscript.exe 进行计数:
    • 如果计数器匹配,则UAC提示可能被拒绝
    • 如果计数器不同,则UAC提示可能已获得批准

如果提升 wscript.exe 异步启动或同时完成,上述方法可能会失败...例如,从 RDP session 或来自任务计划程序等。

option explicit
On Error GoTo 0
Dim strResult: strResult = Wscript.ScriptName

Dim objShell, svcCounter, preCounter, postCounter
Set objShell = CreateObject("Shell.Application")

svcCounter = 0
preCounter = 0
postCounter = 0

Call TestProcess ( "SVC", svcCounter, "svchost.exe" )
strResult = strResult & vbNewLine & Cstr( svcCounter)

If svcCounter = 0 Then
'' elevated already
strResult = strResult & vbNewLine & "UAC prompt not required"
objShell.ShellExecute "wscript.exe" _
, """" & "D:\VB_scripts\SO\37911301.vbs" & """ uac" , "", "runas", 1
Else
preCounter = 0

Call TestProcess ( "pre", preCounter, "wscript.exe" )

' By default, Windows Vista/7/8's UAC prompt is shown on a _secure desktop_
' suspending calling process temporarily:
objShell.ShellExecute "wscript.exe" _
, """" & "D:\VB_scripts\SO\37911301.vbs" & """ uac" , "", "runas", 1

Call TestProcess ( "post", postCounter, "wscript.exe" )

strResult = strResult & vbNewLine & Cstr( preCounter) & vbTab & Cstr( postCounter)
If preCounter = postCounter Then
strResult = strResult & vbNewLine & "UAC prompt PROBABLY refused"
Else
strResult = strResult & vbNewLine & "UAC prompt PROBABLY approved"
End If
End If

Wscript.Echo strResult
Wscript.Quit

Sub TestProcess( byVal sStage, byRef nCounter, byVal strCap)

strResult = strResult & vbNewLine & sStage
Dim strQuery, objWMIService, colItems, objItem, sCaption, sCmdLine
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

strQuery = "SELECT * FROM Win32_Process WHERE Caption = '" & strCap & "'"

Set objWMIService = GetObject("winmgmts:\\" & "." & "\ROOT\CIMV2")
Set colItems = objWMIService.ExecQuery(strQuery _
, "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem in colItems
sCaption = objItem.Caption
sCmdLine = objItem.CommandLine
if VarType( sCmdLine ) = 1 Then nCounter = nCounter + 1 'Null => elevated
strResult = strResult & vbNewLine & sCaption
strResult = strResult & vbTab & objItem.ProcessId
strResult = strResult & vbTab & sCmdLine
'strResult = strResult & vbTab & VarType( sCmdLine ) & vbTab & TypeName( sCmdLine )
Next
End Sub

请注意,strResult 中的所有垃圾仅用于调试目的。而且…我知道用于测试的TestProcess调用如果已经升高对蚊子来说太重了…

关于VBScript 的 ShellExecute 返回代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37948938/

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