如何从 vbs 以管理员身份运行 vbs? [英] How to run vbs as administrator from vbs?
问题描述
任何人都可以帮助我从自身运行 vbs 但具有管理员权限吗?我需要通过 VBScript 用 Windows 8 重命名计算机,但只有当我通过管理员命令行(CMD → 以管理员身份运行 → runScript.vbs)运行我的脚本时才有可能.如果我使用经典 CMD 启动脚本,则计算机不会重命名.
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.
我的想法是我使用用户权限启动脚本,没有参数,如果没有参数,脚本会以管理员权限和参数作为标识符我是管理员"重新运行.
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?
我试过了:
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
推荐答案
如果在计算机上启用了 UAC,则应该可以执行以下操作:
If UAC is enabled on the computer, something like this should work:
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
这篇关于如何从 vbs 以管理员身份运行 vbs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!