windows 如何从 vbs 以管理员身份运行 vbs?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17466681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:39:27  来源:igfitidea点击:

How to run vbs as administrator from vbs?

windowsvbscriptadministratorelevated-privileges

提问by Daphnis

Can anyone help me with running vbs from itself but with administrator rights? I need rename computer with Windows 8 via VBScript, but it's possible only if I run my script through administrator command line (CMD → Run as Administrator → runScript.vbs). If I start script with classic CMD the computer isn't renamed.

任何人都可以帮助我从自身运行 vbs 但具有管理员权限吗?我需要通过 VBScript 用 Windows 8 重命名计算机,但只有当我通过管理员命令行(CMD → 以管理员身份运行 → runScript.vbs)运行我的脚本时才有可能。如果我使用经典 CMD 启动脚本,则计算机不会重命名。

My idea is I start script with user rights, without parameters and if there is no parameter, the script re-runs itself with admin rights and with parameter as identificator "I'm admin".

我的想法是我使用用户权限启动脚本,没有参数,如果没有参数,脚本会以管理员权限和参数作为标识符“我是管理员”重新运行。

Does anyone know how I can do this?

有谁知道我怎么能做到这一点?

Edit:

编辑:

I tried this:

我试过这个:

If WScript.Arguments.Count = 0 Then
    Set objShell = CreateObject("Shell.Application")
    objShell.ShellExecute "wscript.exe", "c:\Users\admin\Documents\selfConfigure.vbs -1", "", runas", 1
End If

回答by Ansgar Wiechers

If UAC is enabled on the computer, something like this should work:

如果在计算机上启用了 UAC,则应该可以执行以下操作:

If Not WScript.Arguments.Named.Exists("elevate") Then
  CreateObject("Shell.Application").ShellExecute WScript.FullName _
    , """" & WScript.ScriptFullName & """ /elevate", "", "runas", 1
  WScript.Quit
End If

'actual code

回答by Hussain

Add this to the beginning of your file:

将此添加到文件的开头:

Set WshShell = WScript.CreateObject("WScript.Shell")
If WScript.Arguments.Length = 0 Then
  Set ObjShell = CreateObject("Shell.Application")
  ObjShell.ShellExecute "wscript.exe" _
    , """" & WScript.ScriptFullName & """ RunAsAdministrator", , "runas", 1
  WScript.Quit
End if

回答by funny

fun lil batch file

有趣的小批处理文件

@set E=ECHO &set S=SET &set CS=CScript //T:3 //nologo %~n0.vbs /REALTIME^>nul^& timeout 1 /NOBREAK^>nul^& del /Q %~n0.vbs&CLS
@%E%off&color 4a&title %~n0&%S%CX=CLS^&EXIT&%S%BS=^>%~n0.vbs&%S%G=GOTO &%S%H=shell&AT>NUL
IF %ERRORLEVEL% EQU 0 (
    %G%2
) ELSE (
    if not "%minimized%"=="" %G%1
)
%S%minimized=true & start /min cmd /C "%~dpnx0"&%CX%
:1
%E%%S%%H%=CreateObject("%H%.Application"):%H%.%H%Execute "%~dpnx0",,"%CD%", "runas", 1:%S%%H%=nothing%BS%&%CS%&%CX%
:2
%E%%~dpnx0 fvcLing admin mode look up&wmic process where name="cmd.exe" CALL setpriority "realtime"& timeout 3 /NOBREAK>nul
:3
%E%x=msgbox("end of line" ,48, "%~n0")%BS%&%CS%&%CX%

回答by Hari Krishna

`My vbs file path :

`我的vbs文件路径:

D:\QTP Practice\Driver\Testany.vbs'

D:\QTP Practice\Driver\Testany.vbs'

objShell = CreateObject("Shell.Application")

objShell.ShellExecute "cmd.exe","/k echo test", "", "runas", 1

set x=createobject("wscript.shell")

wscript.sleep(2000)

x.sendkeys "CD\"&"{ENTER}"&"cd D:"&"{ENTER}"&"cd "&"QTP Practice\Driver"&"{ENTER}"&"Testany.vbs"&"{ENTER}"

--from google search and some tuning, working for me

--来自谷歌搜索和一些调整,对我来说有效

回答by Riccardo La Marca

This is the universal and best solution for this:

这是对此的通用和最佳解决方案:

If WScript.Arguments.Count <> 1 Then WScript.Quit 1
RunAsAdmin
Main

Sub RunAsAdmin()
    Set Shell = CreateObject("WScript.Shell")
    Set Env = Shell.Environment("VOLATILE")
    If Shell.Run("%ComSpec% /C ""NET FILE""", 0, True) <> 0 Then
        Env("CurrentDirectory") = Shell.CurrentDirectory
        ArgsList = ""
        For i = 1 To WScript.Arguments.Count
            ArgsList = ArgsList & """ """ & WScript.Arguments(i - 1)
        Next
        CreateObject("Shell.Application").ShellExecute WScript.FullName, """" & WScript.ScriptFullName & ArgsList & """", , "runas", 5
        WScript.Sleep 100
        Env.Remove("CurrentDirectory")
        WScript.Quit
    End If
    If Env("CurrentDirectory") <> "" Then Shell.CurrentDirectory = Env("CurrentDirectory")
End Sub

Sub Main()
    'Your code here!
End Sub

Advantages:

好处:

1) The parameter injection is not possible.
2) The number of arguments does not change after the elevation to administrator and then you can check them before you elevate yourself.
3) You know for real and immediately if the script runs as an administrator. For example, if you call it from a control panel uninstallation entry, the RunAsAdmin function will not run unnecessarily because in that case you are already an administrator. Same thing if you call it from a script already elevated to administrator.
4) The window is kept at its current size and position, as it should be.
5) The current directory doesn't change after obtained administrative privileges.

1) 无法进行参数注入。
2) 提权后参数个数不变,提权前可以查看。
3) 如果脚本以管理员身份运行,您会立即了解真实情况。例如,如果您从控制面板卸载条目调用它,RunAsAdmin 函数将不会不必要地运行,因为在这种情况下您已经是管理员。如果您从已提升为管理员的脚本中调用它,则同样如此。
4) 窗口应保持在当前大小和位置。
5) 获得管理权限后当前目录不变。

Disadvantages: Nobody

缺点:没人

回答by Amit

Nice article for elevation options - http://www.novell.com/support/kb/doc.php?id=7010269

关于高程选项的好文章 - http://www.novell.com/support/kb/doc.php?id=7010269

Configuring Applications to Always Request Elevated Rights:

将应用程序配置为始终请求提升权限:

Programs can be configured to always request elevation on the user level via registry settings under HKCU. These registry settings are effective on the fly, so they can be set immediately prior to launching a particular application and even removed as soon as the application is launched, if so desired. Simply create a "String Value"under "HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"for the full path to an executable with a value of "RUN AS ADMIN". Below is an example for CMD.

可以将程序配置为始终通过 下的注册表设置请求用户级别的提升HKCU。这些注册表设置即时生效,因此可以在启动特定应用程序之前立即设置它们,甚至可以在应用程序启动后立即删除(如果需要)。只需创建一个"String Value""HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"的完整路径的可执行文件的值"RUN AS ADMIN"。以下是 CMD 的示例。

Windows Registry Editor Version 5.00
[HKEY_Current_User\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers]
"c:\windows\system32\cmd.exe"="RUNASADMIN"