首页 > 编程 > BAT > 正文

admin.vbs 以管理员身份运行程序的vbs命令

2020-07-26 20:21:02
字体:
来源:转载
供稿:网友
复制代码 代码如下:

'以管理员身份运行程序的命令admin.vbs
Set objWMIServices = GetObject("winmgmts:{impersonationLevel=impersonate}!//./root/cimv2")
Set objWbemObjectSet = objWMIServices.ExecQuery(_
"SELECT * FROM Win32_Process WHERE " &_
"ExecutablePath='" & Replace(WScript.FullName,"/","//") & "' and " & _
"CommandLine LIKE '%" & WScript.ScriptName & "%'")
for each objWbemObject in objWbemObjectSet
cmdline = objWbemObject.CommandLine
next
if WScript.Arguments.Count then
file = WScript.Arguments(0)
if file="/?" then
call ShowHelp()
WScript.Quit
end if
Set RegEx = new RegExp
RegEx.IgnoreCase = true
RegEx.Global = true
RegEx.Pattern = "//|//|/||/(|/)|/[|/]|/{|/}|/^|/$|/.|/*|/?|/+"
temp1 = RegEx.Replace(WScript.ScriptName, "/$&")
temp2 = RegEx.Replace(file, "/$&")
RegEx.Global = false
RegEx.Pattern = "^.*?" & temp1 & "[""/s]*" & temp2 & """?/s*"
args = RegEx.Replace(cmdline, "")
'WScript.Echo file, args
else
file = "cmd.exe"
'args = "/k cd /d """ & CreateObject("WScript.Shell").CurrentDirectory & Chr(34)
end if
'核心代码
Set sh = CreateObject("Shell.Application")
call sh.ShellExecute( file, args, , "runas" )
function ShowHelp()
dim HelpStr
HelpStr = "以管理员身份运行程序。" & vbCrLf _
& vbCrLf _
& WScript.ScriptName & " [program] [parameters]..." & vbCrLf _
& vbCrLf _
& "program 要运行的程序" & vbCrLf _
& "parameters 传递给 program 的参数" & vbCrLf _
& vbCrLf
WScript.Echo HelpStr
end function

建议放到%SystemRoot%/system32目录下,对喜欢玩cmd而且操作系统是win7的人有点用处。
偶尔用到某些命令(如reg.exe)需要管理员权限,直接在cmd中 admin reg add ... 即可。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表