标题: [原创] 以管理员身份运行程序的命令admin.vbs [打印本页]
作者: powerbat 时间: 2011-11-13 21:24 标题: 以管理员身份运行程序的命令admin.vbs
- '以管理员身份运行程序的命令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 ... 即可。
作者: 920168 时间: 2011-11-19 14:50
支持,刚装WIN7,不习惯!
作者: awk 时间: 2011-11-19 17:11
怎样以system权限运行一个脚本呢?
作者: saiforbvs 时间: 2013-1-9 17:13
回复 1# powerbat
你好,请问,在此环境下调用START.bat应该插入什么代码?
作者: czjt1234 时间: 2013-1-10 08:18
'在系统为Win7或Vista时提升VBS脚本权限
Sub DoUACRunScript()
Dim objOS
For Each objOS in GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem")
If InStr(objOS.Caption,"XP") = 0 Then
If WScript.Arguments.length = 0 Then
Dim objShell
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "wscript.exe", Chr(34) &_
WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
End If
End If
Next
End Sub
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |