Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expect and Spawn with PowerShell

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 ?

like image 543
POLLOX Avatar asked Aug 08 '26 06:08

POLLOX


1 Answers

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
like image 144
s31064 Avatar answered Aug 10 '26 20:08

s31064



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!