如何在Windows中运行一个批处理命令为不输入密码不同的用户? [英] How to run a batch command in Windows as a different user without typing the password?

查看:1630
本文介绍了如何在Windows中运行一个批处理命令为不输入密码不同的用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行作为一个窗口一个班轮命令提示符简单的Windows批处理命令,而无需指定密码。用户名为亚历克斯,所以我尝试:

I am trying to run a simple windows batch command as a one-liner on a windows command prompt, without having to specify the password. The user is named 'alex', so I am trying:

>cmd /C echo PASSWD | runas /profile /user:alex "cmd /c dir"

但我得到以下错误:

but I get the following error:

Enter the password for alex:
Attempting to start cmd /c dir as user "ALEX-NEU\alex" ..
RUNAS ERROR: Unable to run - cmd /c dir
1326: Logon failure: unknown user name or bad password.

其中, ALEX_NEU 是计算机名称。用户名和密码提供的是正确的 - 所以为什么我得到这个错误?如何正确呢?

where ALEX_NEU is the machine name. The user name and the provided password are correct - so why do I get this error? How to do it correctly?

推荐答案

在运行时提供密码运行方式的唯一方法是用一个脚本,不是非常优雅无论是。这是丑陋的,它的hackish,并且它要求的安全隐患。话虽如此,我已经做了,在过去类似的东西与 WScript.Shell .RUN .SendKeys 方法是这样的:

The only way to supply the password to runas at runtime is with a script, and not terribly gracefully either. It's ugly, it's hackish, and it's asking for security troubles. Having said that, I've done something similar in the past with WScript.Shell's .run and .SendKeys methods like this:

@if (@a==@b) @end /* multiline JScript comment

:: runas.bat
:: runas with automated password entry

@echo off
setlocal enabledelayedexpansion

set "USER=username"
set "PASS=password"
set "CMD=cmd /c dir && pause"

:: delayed expansion prevents special characters from being evaluated
cscript /nologo /e:JScript "%~f0" !USER! !PASS! !CMD!

goto :EOF

:: end batch portion / begin JScript portion */

for (var i=0, args=[]; i<WSH.Arguments.length; i++)
    args.push(WSH.Arguments(i).replace(/"/g, '^"')); // escape all quotes

var user = args[0],
    pass = args[1],
    cmd = ' "' + args.slice(2).join(' ') + '"',
    oShell = new ActiveXObject("Wscript.Shell");

oShell.run('runas /noprofile /netonly /user:' + user + cmd);
WSH.Sleep(500);
oShell.SendKeys(pass + '{ENTER}');

这是一个黑客,只是一个概念证明,并在很大程度上是未经考验的。我不知道是否会在!CMD!变量处理援引论据。我敢说,如果你想使用它的实际应用中,你必须在你未来的修改和调试很大。

This is a hack, only a proof of concept, and is largely untested. I have no idea whether it'll handle "quoted arguments" in the !CMD! variable. I daresay if you want to use it for practical applications, you'll have a great deal of revising and debugging in your future.

有一些其他的装饰这种方法你可能会考虑。

There are a couple of other embellishments to this method you might consider.

这篇关于如何在Windows中运行一个批处理命令为不输入密码不同的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆