Powershell-与可执行文件的命令行提示进行交互? [英] Powershell - Interact with executable's command line prompts?

查看:239
本文介绍了Powershell-与可执行文件的命令行提示进行交互?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to automate some tasks against a legacy application. It doesn't have an API, but the vendor offers a .exe I can run to perform various tasks against the application.

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

I can call the .exe from within PowerShell just fine, but after calling it it prompts for the following input:


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

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

I can run this interactively from the command prompt just fine and manually enter the requested input as prompted, but I need to automate this in a script. The .exe doesn't accept any command line parameters for this either(it's an old app), the only parameters I can pass are the commands I want to execute against the application. The .exe is only command line based with no GUI, so GUI automation tools aren't an option either.

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

I could do this easily with expect in a *nix shell, however given that I have a .exe I need to run, that's out of the question. There doesn't appear to be an "expect" equivalent in Windows, so I'm curious if this can be accomplished in PowerShell?

注意:对于Windows脚本,我更喜欢PowerShell,但是在必要时可以使用VBScript.

Note: For Windows scripts I prefer PowerShell, but could utilize VBScript if necessary.

推荐答案

如果我没记错的话,如果只是一行,则可以将字符串通过管道传递给exe. "asdf" | example.exe,但是您可能需要使用SendKeys多行输入

If I remember correctly, if it's just one line you can pipe a string to an exe eg. "asdf" | example.exe, but it's multiple lines of input you might need to use 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~")

这篇关于Powershell-与可执行文件的命令行提示进行交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆