用于以管理员身份运行批处理文件而不提示输入密码的脚本

时间:2012-10-10 14:10:11

标签: vbscript

以下脚本按预期在Windows XP上运行。脚本abc.bat将以管理员身份运行,不会提示输入密码(密码是自动提供的)。但是同样的脚本无法在我们的Windows 7 Box上运行。是否需要进行任何更改?我没有使用VB脚本的经验。谢谢!

Option Explicit

Const USER               = "administrator"
Const PASS               = "*********"
Const WSNAME_COMMANDLINE = "C:\support\abc.bat"

Dim fso         : Set fso = CreateObject("Scripting.FileSystemObject")
Dim WSHShell    : Set WshShell = CreateObject("WScript.Shell")
Dim WshNetwork  : Set WshNetwork = CreateObject("WScript.Network")
Dim oDic        : Set oDic = CreateObject("Scripting.Dictionary")
Dim objArgs     : Set objArgs = WScript.Arguments
Dim oProcessEnv : Set oProcessEnv = WshShell.Environment("PROCESS")

Dim sPathToRunAs, iReturnCode

sPathToRunAs = oProcessEnv("SystemRoot")&"\System32\runas.exe"
''msgbox sPathtorunas

if Not fso.FileExists(sPathToRunAs) Then : WScript.Quit(1) 'Can't find RunAs

'''msgbox "runas /user:" & USER & " " & CHR(34) & WSNAME_COMMANDLINE & CHR(34)
iReturnCode=WshShell.Run("runas /user:" & USER & " " & CHR(34) & WSNAME_COMMANDLINE & CHR(34), 2, FALSE)

Wscript.Sleep 40                  ' Time for window to open.
WshShell.AppActivate(sPathToRunAs)' Activate the Window
Wscript.Sleep 3
WSHShell.SendKeys PASS & "~"      ' Send the password
Wscript.Sleep 3

''msgbox "done"

1 个答案:

答案 0 :(得分:0)

您是否在Windows 7上尝试禁用UAC(用户帐户控制)的脚本?

您可以在此处找到关于禁用UAC的方法:http://windows.microsoft.com/en-US/windows-vista/Turn-User-Account-Control-on-or-off

如果UAC是您遇到问题的原因,您可以使用Windows任务计划程序来避免这种情况,如下所述:http://poundcomment.wordpress.com/2011/03/18/how-to-create-a-whitelist-uac-for-windows-7/