【发布时间】:2023-02-13 23:58:18
【问题描述】:
是否可以以管理员权限运行部分 hta 文件?我想以一种方式实现,当我单击“添加时间”按钮时,它应该提示我提供管理员详细信息。我尝试了几种方法但没有用。我尝试了一些方法,但它给了我一个错误,其他人不会响应以管理员身份运行的调用。我将感谢你的帮助。
这是文件
<!DOCTYPE html>
<html>
<title >Time remaining</title>
<head>
<meta charset="UTF-8" http-equiv="X-UA-Compatible" content="IE=9">
<hta:application
applicationname="Time remaining"
id=oHTA
maximizebutton=no
windowstate="normal"
scroll=no
SysMenu=no
>
<script language="VBScript">
Const DefaultWait = 30 'minutes
Const LogoffCmd = "Shutdown.exe /l /f"
Const RestartCmd = "Shutdown.exe -r -f"
Const ShutdownCmd = "shutdown.exe /s /t"
Const Logoff = True
Const Unattended = True
Const TestMode = False
Const HKCU = &H80000001
Set oWSH = CreateObject("Wscript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oReg = GetObject("winmgmts:\\.\root\default:StdRegProv")
Dim TimeLeftInSeconds,WaitTimer,Wait,PrevWait
MyPath = Mid(document.URL,8)
MyName = oFSO.GetFileName(MyPath)
MyFolder = oFSO.GetParentFolderName(MyPath)
oWSH.CurrentDirectory = MyFolder
document.Title = MyName
Scale = GetScale()
w = 300 * Scale: h = 250 * Scale
Window.ResizeTo w, h
Window.MoveTo (screen.availWidth - w)/2, (screen.availHeight - h)/2
Sub RunAsAdmin
If InStr(LCase(oHTA.commandLine), " /elevated") = 0 Then
createobject("Shell.Application").ShellExecute "mshta.exe", oHTA.commandLine & " /elevated", "", "runas", 4
self.close
End If
End Sub
Sub window_onLoad
ShutdownOption(0).style.zoom = Scale
ShutdownOption(1).style.zoom = Scale
ShutdownOption(2).style.zoom = Scale
Wait = DefaultWait
WaitBox.Value = Wait
TimeLeftInSeconds = Wait * 60
WaitBox.select
If Unattended Then
UpdateCountdown
WaitTimer = Window.SetInterval("UpdateCountdown()", 1000)
End If
ShutdownOption(0).checked = True
If Restart Then ShutdownOption(1).checked = True
If Shutdown Then ShutdownOption(2).checked = True
End Sub
Sub document_onKeyDown
If window.event.keyCode=13 Then RestartCountdown
End Sub
Sub ReSelectInput
WaitBox.select
End Sub
Sub UpdateCountdown
Hours = CInt(TimeLeftInSeconds \ 3600)
Minutes = CInt((TimeLeftInSeconds Mod 3600) \ 60)
Seconds = TimeLeftInSeconds Mod 60
CountDown.innerHTML = Hours & ":" & Right("0" & Minutes,2) & ":" & Right("0" & Seconds,2)
If TimeLeftInSeconds<=0 Then
Cmd = LogoffCmd
If ShutdownOption(1).checked Then Cmd = RestartCmd
If ShutdownOption(2).checked Then Cmd = ShutdownCmd
If TestMode Then
MsgBox Cmd
Else
oWSH.Run Cmd,1,False
End If
self.Close
Exit Sub
End If
TimeLeftInSeconds = TimeLeftInSeconds - 1
End Sub
Sub RestartCountdown
If WaitTimer="" Then WaitTimer = Window.SetInterval("UpdateCountdown()", 1000)
WaitBox.select
If Not IsNumeric(Replace(WaitBox.Value,":",".")) Then
WaitBox.Value = PrevWait
WaitBox.select
Exit Sub
End If
PrevWait = WaitBox.Value
Wait = WaitBox.Value
If InStr(Wait,":")>0 Then
aWait = Split(Wait,":")
Wait = aWait(0)*60 + aWait(1)
End If
TimeLeftInSeconds = Wait * 60
UpdateCountdown
End Sub
Function GetScale()
GetScale = 1.0
Value = oWSH.RegRead("HKCU\Control Panel\Desktop\WindowMetrics\AppliedDPI")
If Value > 96 Then
'Custom scaling is set
GetScale = Value/96
Else
'See if standard scaling is set
Key = "Control Panel\Desktop\PerMonitorSettings"
Result = oReg.EnumKey(HKCU, Key, ArrKeys)
If Result=0 Then
'Assume first monitor in list is the one we want
For Each SubKey In ArrKeys
Exit For
Next
Value = oWSH.RegRead("HKCU\" & Key & "\" & SubKey & "\DPIValue")
If Value>0 Then GetScale = 1 + (Value * 0.25)
End If
End If
End Function
</script>
<style>
.body {background-color:Lavender; font-family:Segoe UI; font-size:11pt, justify-content: center;}
h1 {color:red; text-align: center;}
.button {width:6em}
.radio {vertical-align:bottom}
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 0px; /* Location of the box */
left: 0;
top: 0;
width: 90%; /* Full width */
height: 60%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
/* Modal Content */
.modal-content {
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
text-align: center;
}
/* The Close Button */
.close {
color: #aaaaaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
</style>
</head>
<body>
<div class="timer"> <h1 id=CountDown>  </h1></div>
<button id="myBtn" value="Add Time" onClick=RunAsAdmin()> Add Time</button>
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
Enter minutes to be added<br><br>
<input type=text size=8 id=WaitBox>
<input type=button class=button id=OKButton value="OK" onClick=RestartCountdown()><br><br>
<input type=radio class=radio name=ShutdownOption onClick=ReSelectInput()>Logoff 
<input type=radio class=radio name=ShutdownOption onClick=ReSelectInput()>Restart 
<br><input type=radio class=radio name=ShutdownOption onClick=ReSelectInput()>Shutdown
</div>
</div>
<script>
var modal = document.getElementById("myModal");
var btn = document.getElementById("myBtn");
var span = document.getElementsByClassName("close")[0];
btn.onclick = function() {
modal.style.display = "block";
}
span.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
</body>
</html>
【问题讨论】:
-
那是很多代码。您应该发布MRE。我认为最简单的方法是将添加时间提示放在单独的 HTA 中并使用
Runas运行它并通过 HKLM 中的条目返回值。我将很快发布一个最小的例子。 -
顺便说一句,GetScale 函数中有一个错误。可以在here 找到该函数的更新版本。
-
我对此非常感激。这是我尝试了一段时间的方法,但效果很好。我也在问是否可以运行计时器,因为当我尝试更改值时它保持静态。提前致谢。
-
请参阅下面的答案以获取完整的解决方案。这为您提供了一个完全可用的倒计时计时器,并带有 UAC 提示以增加更多时间,但我很好奇您打算如何防止用户使用任务管理器(或命令提示符下的 TaskKill)终止主脚本?
-
@user692942 我检查了重复项,但许多技术是特定于 VBScript 的(例如,使用
wscript对象),而且我看到,没有一个技术会导致用户在 UAC 提示符下单击“否”。