7

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

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

Emperor XLII
  • 13,014
  • 11
  • 65
  • 75
GregWringle
  • 445
  • 3
  • 6
  • 16
  • Could you add some of your code? Is the program asking for anything more than a password? Could you simply try `echo password > program.exe`. Take a look here - http://stackoverflow.com/questions/18972156/how-to-pass-input-to-exe-in-batch-file. – unclemeat Jan 06 '15 at 22:50

3 Answers3

3

There is nothing built into PowerShell. However there is a module that should be able to do this called Await - https://msconfiggallery.cloudapp.net/packages/Await/

There is a PowerShell Summit session on how to use Await here - https://www.youtube.com/watch?v=tKyAVm7bXcQ

Keith Hill
  • 194,368
  • 42
  • 353
  • 369
3

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

* 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.

Community
  • 1
  • 1
Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
1

Sorry, I don't know how linux's expect works... However, at this post there is a Batch-file solution for a problem similar to yours and titled "Automatically respond to runas from batch file":

@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}");
Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • `SendKeys` is pretty fragile, mostly because of window focus, and in my (more extensive than I'd really like here) experience it's best to avoid fiddling with it unless there's no choice. It's also more prone to e.g. keyloggers, although that's probably not more than a minor security note; security against hostile code would need a lot more effort than this question seems to assume. – Nathan Tuggy Jan 12 '15 at 23:22