admin.vbs 以管理员身份运行程序的vbs命令
程序员文章站
2022-03-22 09:45:15
复制代码 代码如下: '以管理员身份运行程序的命令admin.vbs set objwmiservices = getobject("winmgmts:{impersona...
复制代码 代码如下:
'以管理员身份运行程序的命令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 ... 即可。
下一篇: Docker 命令自动补全?要的