Powershell - 与可执行文件的命令行提示进行交互?

Powershell - Interact with executable's command line prompts?

我需要针对遗留应用程序自动执行一些任务。它没有 API,但供应商提供了一个 .exe,我可以 运行 对应用程序执行各种任务。

我可以从 PowerShell 中调用 .exe 就好了,但在调用它之后它会提示输入以下内容:

Do you agree to the license: YES
User name: myuid
Password: SuperSecretPassword`

我可以 运行 在命令提示符下以交互方式执行此操作,然后根据提示手动输入请求的内容,但我需要在脚本中自动执行此操作。 .exe 也不接受任何命令行参数(这是一个旧应用程序),我唯一可以传递的参数是我想针对应用程序执行的命令。 .exe 只是基于命令行,没有 GUI,因此 GUI 自动化工具也不是一个选项。

我可以使用 *nix shell 中的 expect 轻松完成此操作,但是鉴于我有一个 .exe,我需要 运行,这是不可能的。 Windows 中似乎没有 "expect" 等价物,所以我很好奇这是否可以在 PowerShell 中完成?

注意:对于 Windows 脚本,我更喜欢 PowerShell,但如果需要可以使用 VBScript。

如果我没记错的话,如果它只是一行,你可以将一个字符串传递给一个 exe,例如。 "asdf" | example.exe,但它是多行输入,您可能需要使用 SendKeys

Add-Type -AssemblyName 'System.Windows.Forms'
$ID = (Start-Process example.exe -PassThru).id 
Sleep 1
Add-Type -AssemblyName Microsoft.VisualBasic 
[Microsoft.VisualBasic.Interaction]::AppActivate([Int32]$ID)
[System.Windows.Forms.SendKeys]::SendWait("YES~")
[System.Windows.Forms.SendKeys]::SendWait("myuid~")
[System.Windows.Forms.SendKeys]::SendWait("SuperSecretPassword~")