【问题标题】:Windows expect command equivalentWindows 期望命令等效
【发布时间】:2015-03-04 16:53:12
【问题描述】:

linux 在 windows 中的期望是什么?有没有办法让脚本在上一个命令提示时发送密码?

runas /user:admin net user username /add

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

【问题讨论】:

标签: powershell batch-file


【解决方案1】:

对不起,我不知道 linux 的期望是如何工作的......但是,this post 有一个批处理文件解决方案,用于解决与您类似的问题,标题为“自动响应批处理文件中的 runas”:

@if (@CodeSection == @Batch) @then

@echo off

start "" runas /user:testuser c:/path/to/my/program.exe
CScript //nologo //E:JScript "%~F0"
goto :EOF

@end

WScript.CreateObject("WScript.Shell").SendKeys("password{ENTER}");

【讨论】:

  • SendKeys 非常脆弱,主要是因为窗口焦点,并且在我(比我在这里真正想要的更广泛)的经验中,除非别无选择,否则最好避免摆弄它。它也更容易出现例如键盘记录器,尽管这可能只是一个次要的安全说明;针对恶意代码的安全性需要比这个问题似乎假设的更多的努力。
【解决方案2】:

据我所知,在 Windows 中没有直接等效于 expect 的方法,尽管您无疑可以轻松编写一个。幸运的是,对于在 PowerShell 中提供凭据的特殊情况,有一种更好的方法,它甚至不在脚本中以明文形式存储密码。 This answer to Batch scripting, Powershell, and not triggering the UAC in Windows* 首先将用户密码保存为安全字符串**:

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

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

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 等。

【讨论】:

    【解决方案3】:

    PowerShell 中没有内置任何内容。但是有一个模块应该能够做到这一点,称为Await - https://msconfiggallery.cloudapp.net/packages/Await/

    这里有一个关于如何使用 Await 的 PowerShell 峰会 - https://www.youtube.com/watch?v=tKyAVm7bXcQ

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-29
      • 1970-01-01
      • 1970-01-01
      • 2016-10-02
      • 2010-09-08
      • 1970-01-01
      • 1970-01-01
      • 2014-08-05
      相关资源
      最近更新 更多