Windows期望命令等效 [英] Windows expect command equivalent

查看:64
本文介绍了Windows期望命令等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相当于Windows在Windows中的期望值是什么?上一条命令提示时,脚本是否可以发送密码?

What is the equivalent to linux's expect in windows? Is there a way for a script to send a password when prompted to by a previous command?

runas /user:admin net user username /add

尝试echo XYZ | runas ...我得到了1326 Logon failure: unknown user name or bad password.该命令在没有管道回声的情况下有效.

Trying echo XYZ | runas ... I got 1326 Logon failure: unknown user name or bad password. The command works without the piped echo.

推荐答案

虽然您可以毫无困难地编写一个Windows,但是我所知道的Windows中没有与expect直接等效的东西.幸运的是,对于在PowerShell中提供凭据的特殊情况,有一种更好的方法,一种方法甚至不在脚本中以明文形式存储密码. 对批处理脚本,Powershell而不是在Windows中不触发UAC的答案 *首先将用户密码保存为安全的位置字符串**:

There is no direct equivalent to expect in Windows that I know of, although you can doubtless write one without too much difficulty. Fortunately, for the special case of supplying credentials in PowerShell, there's a better way, one which doesn't even store passwords in plaintext in the script. This answer to Batch scripting, Powershell, and not triggering the UAC in Windows* starts by saving the user password somewhere as a secure string**:

$pass = Read-Host -AsSecureString
ConvertFrom-SecureString $pass | Out-File pass.txt

然后使用存储的密码以管理员身份运行程序,如下所示:

Then running the program as administrator with the stored password this way:

function Invoke-ElevatedCommand([String]$FileName, [String]$Arguments) {
    $pass = Import-SecureString (Get-Content pass.txt)
    $startinfo = New-Object System.Diagnostics.ProcessStartInfo
    $startinfo.UserName = "administrator"
    $startinfo.Password = $pass
    $startinfo.FileName = $FileName
    $startinfo.Arguments = $Arguments
    [System.Diagnostics.Process]::Start($startinfo)
}
Invoke-ElevatedCommand "net" "user username /add"   # Or whatever you need

* 请注意,这几乎是一个骗局,但并非完全如此,因为公认的答案与该问题并不特别相关.链接的答案仅适合此目的,因此进行了相应的修改.
** 请注意,尽管pass.txt不是明文,但将其放置在任何人都可以抓住它的地方并不安全.在其上保留一些限制性的NTFS权限,例如EFS等.

* Note that this is almost a dupe, but not quite, since the accepted answer is not particularly relevant to this question. Linked answer lightly adapted for the purpose, and upvoted accordingly.
** Note further that, while pass.txt is not plaintext, it's not safe to leave lying around where just anyone can grab it. Stick some restrictive NTFS permissions on it, maybe EFS, etc.

这篇关于Windows期望命令等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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