2016-06-21 67 views
2

有可能有一個返回碼(一個Bool也許?)來知道UAC對話框是否被接受或拒絕?我現在不做如下檢查。VBScript的ShellExecute返回碼

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

如果用戶接受uac對話框(如果有的話),我會很高興。

+1

也許並不完全是重複的,但是這似乎高度相關:http://stackoverflow.com/q/ 27222097/4996248 –

+0

我必須忍受PEBKAC,因爲我看不到他的實際答案。他只是貼了一個手冊部分,但沒有回答OP的問題......實際上。 – CDoc

+0

也許它沒有幫助,但接受的答案似乎可能會給你一些想法。如果不是,希望別人能給你一個更好的答案。 –

回答

1

數十說:basically, no, what you're asking isn't possible.然而,存在其中回答與高可能性問題雖然還有非零假響應的概率一種解決方法。這裏有這樣的解決方案的草案,請參見下面的腳本:

  • 如果升高已經那麼答案是100%:UAC提示不需要
  • 否則,算上升高wscript.exe之前的UAC提示和後:
    • 如果計數器再搭配UAC提示是可能拒絕;
    • 如果計數器不同,則UAC提示是可能是批准

以上如果高架wscript.exe以異步方式啓動或在此期間完成...例如,從RDP會話或任務計劃程序等

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呼叫如果升高已經是對蚊子太重炮......

+0

這涵蓋了我100%的需求。非常感謝你的這個工作周! – CDoc

0

儘管documentation表示否則ShellExecute實際上不返回任何內容,因爲該方法異步運行(即立即返回而不等待新進程終止)。 VBScript中的同步調用可以使用RunExec方法進行,但這些方法不允許提升權限。

基本上,不,你問的是不可能的。的倍