这是另一种选择:
wmic /user:username /password:pass process call create "cmd /c \"d:\\path\\to\\program.exe\" /arg etc"
编辑:显然这不允许在本地机器上使用单独的凭据进行身份验证。
有一种方法可以使用 vbscript 调用 runas 并让 vbscript 将密码发送到控制台以自动输入密码。
set WshShell = WScript.CreateObject("Wscript.Shell")
WshShell.run "runas /noprofile /user:USERNAME " + Chr(34) + "d:\path\to\command.exe /args" + Chr(34)
WScript.Sleep 500
WshShell.SendKeys "PASSWORD"
WshShell.SendKeys "{ENTER}"
set WshShell = nothing
将其保存到.vbs 文件并通过cscript /nologo script.vbs 调用它
如果您需要从批处理脚本运行,只需做一些创造性的回应。
@echo off
setlocal
set username=username
set password=password
set program=d:\path\to\program.exe /arg argument
echo set WshShell = WScript.CreateObject(^"Wscript.Shell^")>runas.vbs
echo WshShell.run ^"runas /netonly /noprofile /user:%username% ^" + Chr(34) + ^"%program%^" + Chr(34)>>runas.vbs
echo WScript.Sleep 500>>runas.vbs
echo WshShell.SendKeys ^"%password%^">>runas.vbs
echo WshShell.SendKeys ^"{ENTER}^">>runas.vbs
echo set WshShell = nothing>>runas.vbs
cscript /nologo runas.vbs
del /q runas.vbs
如果这对您不起作用,您还可以使用 psexec 运行具有不同凭据的程序。
psexec -u USERNAME -p PASSWORD d:\path\to\command.exe
我能想到的唯一其他选择是通过组策略启动脚本运行您的脚本,该脚本将从系统帐户执行脚本。我也考虑过从注册表的HKLM\Software\Microsoft\Windows\CurrentVersion\RunOnce 调用它,但我认为这可能会从重启后第一个登录的用户启动它。