Is there any way to do expect and spawn with powershell. I have to parse a CLI program with powershell which is asking for password is there any way to input this password via powershell. In perl or python even in bash you can use expect/spawn Is there any solution in powershell ?
Asked
Active
Viewed 1,387 times
2 Answers
1
One way to do this would be to create a text file with the encrypted password one time, then call this file as the password in as many scripts as necessary.
Create the password file once:
$pwd = Read-Host 'Enter password for encrypting:' -AsSecureString | ConvertFrom-SecureString | Out-File -Path 'C:\SpecialFiles\CLIPassword.txt'
Then you can use it whenever it's needed:
$pass = (Get-Content -Path 'C:\SpecialFiles\CLIPassword.txt' | ConvertTo-SecureString)
$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList (<insert CLI UserName>, $pass)
Then when you need to supply the username and password to your script, you simply pipe
$creds | <command>
or if the command supports the -Credential parameter
<command> -Credential $creds
If your command needs the user name and password entered separately, you can do that by specifying the property name:
<command> -UserName $creds.UserName -Password $creds.Password

s31064
- 147
- 2
- 4
-1
You can use Read-Host to prompt the user for input. See here for more information.
$pass = Read-Host 'What is your password?' -AsSecureString
$decodedpass = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass))
I'm sure what you want to do with spawn, but you can execute other scripts or executables by just calling them
.\MyOtherScript.ps1

Community
- 1
- 1

Lars Truijens
- 42,837
- 6
- 126
- 143
-
1Hi, I don´t know if I don´t explain the situation clearly, I want avoid the user interaction. The program which powershell calls is asking me for the password and I want avoid this step, usually Linux to this situation I use Expect and Spawn to send keystrokes to the nested program. – POLLOX Jul 31 '13 at 15:54