2015-09-26 53 views
3

在wsf,vbs和js文件中,您可以輕鬆瞭解它們是否運行提升,如果不運行,您可以輕鬆製作它們。
我該寫的代碼是這樣的:使HTA文件以管理員身份運行(提升)

EnsureElevatedPrivileges(); 
WScript.Echo("Running elevated now!"); 

function EnsureElevatedPrivileges() { 
    if (!WScript.Arguments.Named.Exists("elevate")) { 
     new ActiveXObject("Shell.Application").ShellExecute(WScript.FullName, "\"" + WScript.ScriptFullName + "\" /elevate", "", "runas", 1); 
     WScript.Quit(); 
    } 
} 

但由於全球WScript的變量不存在有這種不HTA文件。
那麼我如何強制一個特定的HTA文件作爲管理員運行(本身)?

回答

3

您可以在此看一看:Procedure to run HTA elevated

<html> 
<head> 
<title>HTA Helpomatic</title> 

<HTA:APPLICATION 
    ID="oHTA" 
    APPLICATIONNAME="HTAHelpomatic" 
    SCROLL="yes" 
    SINGLEINSTANCE="yes" 
> 
<!-- ID="objHTAHelpomatic" --> 
<!-- WINDOWSTATE="maximize" --> 

</head> 

<SCRIPT Language="VBScript"> 

If HTAElevate() = True Then 
    CreateObject("WScript.Shell").Run "mmc.exe compmgmt.msc", , True 
    Call Main() 
End If 

Sub Main() 
    MsgBox "HTA-Ende", 4096 
End Sub 


'*** v13.3 *** www.dieseyer.de ***************************** 
Function HTAElevate() 
'*********************************************************** 
' Unter Windows x64 laufen VBS' nach einem Doppelklick in der x64-Umgebung 
' mit %WinDi%\System32\wscript.exe oder mit %WinDi%\System32\cscript.exe. 
' In der x64-Umgebung laufen VBS aber nicht (richtig). Die Prozedur 
' HTAElevate() erkennt dies und startet ggf. das VBS in der 

    Const Elev = " /elevated" 

' MsgBox oHTA.commandLine, , "5016 :: " 

' Trace32Log "5018 :: oHTA.commandLine: ==" & oHTA.commandLine & "==", 1 

    HTAElevate = True 

' If InStr(LCase(oHTA.commandLine), Elev) > 0 then MsgBox oHTA.commandLine, , "5022 :: " 
    If InStr(LCase(oHTA.commandLine), Elev) > 0 then Exit Function 


    On Error Resume Next 
    window.resizeto 750, 10 ' : window.moveto screen.width/2, screen.height/2 
    On Error GoTo 0 

' MsgBox oHTA.commandLine, , "5030 :: " 

    createobject("Shell.Application").ShellExecute "mshta.exe", oHTA.commandLine & Elev, "", "runas", 1 

    HTAElevate = False 

    self.close 

End Function ' HTAElevate() 


</SCRIPT> 
<body> 


</body> 
</html> 
相關問題