2013-07-31 52 views
3

有没有什么办法可以期待和产生PowerShell。 我必须使用powershell解析CLI程序,它需要输入密码,有没有什么方法可以通过PowerShell输入密码。 即使在bash中,perl或python也可以使用expect/spawn 在powershell中有没有解决方案?用PowerShell期待和产卵

回答

-1

您可以使用Read-Host提示用户输入。有关更多信息,请参见here

$pass = Read-Host 'What is your password?' -AsSecureString 
$decodedpass = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass)) 

我敢肯定,你想与重生的事,但你可以通过只调用他们做这将是创建一个文本文件

.\MyOtherScript.ps1 
+1

嗨, 我不知道如果我不解释情况很清楚,我想避免用户交互。 powershell调用的程序是要求输入密码,我希望避免这一步,通常Linux使用Expect和Spawn将这些情况发送到嵌套程序。 – POLLOX

1

一种方式执行其他脚本或可执行文件使用加密密码一次,然后根据需要在多个脚本中将该文件作为密码进行调用。

创建密码文件一次:

$pwd = Read-Host 'Enter password for encrypting:' -AsSecureString | ConvertFrom-SecureString | Out-File -Path 'C:\SpecialFiles\CLIPassword.txt' 

然后你可以使用它时,它的需要:当你需要提供给脚本的用户名和密码

$pass = (Get-Content -Path 'C:\SpecialFiles\CLIPassword.txt' | ConvertTo-SecureString) 
$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList (<insert CLI UserName>, $pass) 

然后,你只需管

$creds | <command> 

或者如果该命令支持-Credential参数

<command> -Credential $creds 

如果你的命令需要用户名和密码分别输入,你可以做到这一点通过指定属性名称:

<command> -UserName $creds.UserName -Password $creds.Password