以管理员身份运行HTA文件(提升)

时间:2015-09-26 17:12:09

标签: jscript hta wsh elevated-privileges

在wsf,vbs和js文件中,您可以轻松找到它们是否升高,如果不是,您可以轻松制作它们。
我为此编写的代码就是:

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();
    }
}

但这在HTA文件中不起作用,因为那里不存在全局WScript变量 那么我怎么能强制一个特定的HTA文件运行(本身)作为管理员?

2 个答案:

答案 0 :(得分:4)

您可以看一下: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>

答案 1 :(得分:0)

<html>
<head>
    <SCRIPT LANGUAGE="VBScript" use="to move window offscreen so that it should not flicker">
        X=top.screenleft
        Y=top.screentop
        window.moveTo -2000,-2000
    </SCRIPT>
    <hta:application
      ID="thisHta"
      ApplicationName="The Great ManojBhakarPCM"
      Border="thin"
      BorderStyle="Static"
      Caption="Yes"
      ContextMenu="No"
      MaximizeButton="No"
      MinimizeButton="No"
      Navigable="No"
      Scroll="yes"
      Selection="yes"
      ShowInTaskbar="Yes"
      SingleInstance="no"
      SysMenu="Yes"
      WindowState="normal"
    >
    <SCRIPT Language="VBScript" use="to Run As Administrator And window onload.">
        If InStr(LCase(thisHta.commandLine), " /elevated") = 0 Then
            createobject("Shell.Application").ShellExecute "mshta.exe", thisHta.commandLine & " /elevated", "", "runas", 4
            self.close
        else
            window.moveTo X,Y
        End If
        Sub Window_OnLoad 'On window loading elements compeleted. start work here.
          Document.body.innerHtml = "hello public, to kaise hai aap log? तो कैसे है आप लोग ?"
        End Sub
    </SCRIPT>
    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"  />
</head>

<style>
    /* Author: ManojBhakarPCM */
    body {
          font-family: Arial, Helvetica, sans-serif;
    }
    .tile{/* Tile: width:vary, height: content wrap, padding: 5px;*/
        height: auto !important;
        padding:10px;
        margin:3px;
        display:inline-block;
    }
    .tilehead{
        height: auto !important;
        padding-left:10px;
        padding-right:10px;
        padding-top:2px;
        padding-bottom:2px;
        margin:2px;
        font-size:10px;
        display:inline-block;
    }
    /*-----COLORS----*/
    .pink{background-color:#FF4081;color:white;}
    .red{background-color:#D32F2F;color:white;}
    .purple{background-color:#E040FB;color:white;}
    .deeppurple{background-color:#512DA8;color:white;}
    .indigo{background-color:#536DFE;color:white;}
    .blue{background-color:#1976D2;color:white;}
    .lightblue{background-color:#0288D1;color:white;}
    .cyan{background-color:#00BCD4;color:white;}
    .teal{background-color:#00796B;color:white;}
    .green{background-color:#388E3C;color:white;}
    .lime{background-color:#CDDC39;color:white;}
    .yellow{background-color:#FFEB3B;color:white;}
    .darkyellow{background-color:#FBC02D;color:white;}
    .orange{background-color:#FF5722;color:white;}
    .bluegray{background-color:#607D8B;color:white;}
    .gray{background-color:#333;color:white;}
    
    .roundcorner{border-radius:5px;}
    .bold{font-weight:bold;}
    .padmin{padding:5px;}
    .padmid{padding:10px;}
    .padfull{padding:15px;}
    .inline{display:inline-block;}
    /* --------CONTROLS---------*/
    /*NAVBAR*/
    
    .navbar {overflow: hidden;background-color: #333;padding:3px;text-decoration: none;}
    .navbar a{padding:5px;font-size: 10px;text-decoration: none;}
    .navbar a:hover{background-color:red;}

    table {
      font-family: arial, sans-serif;
      border-collapse: collapse;
      width: 50%;
    }

    td, th {
      border: 1px solid #dddddd;
      text-align: left;
      padding: 8px;
    }

    tr:nth-child(even) {
      background-color: #dddddd;
    }
</style> 
<body> 

Hellow Worlds
</body> 
</html>

我创建了这个hta模板,我经常使用它。我相信模板是处理脚本等的最佳方式。我通常将这些模板上传到网站,因此在任何计算机(不是我的计算机)上工作时,只需下载并开始编码即可完成工作。 特点:

  • 以管理员身份运行
  • 不闪烁。
  • 在获得管理员权限前什至不显示。
  • Unicode支持。
  • 所有HTA标签。
  • 使用CSS的完整类来实现平滑的UI