自动执行 PowerShell 响应以提示 [英] Automate PowerShell Response to prompt
问题描述
创建一个调用应用程序 cmdlet 的 PowerShell 脚本.当 cmdlet 运行时,它会提示响应并且脚本挂起.cmdlet 没有用于此的任何参数.有没有办法以编程方式响应?我试过 ECHO 但这不起作用.脚本挂起
Creating a PowerShell script that calls an apps cmdlet. When then cmdlet runs it prompts for a response and the scripts hangs. The cmdlet doesn't have any parameters for this. Is there any way to respond programmatically? I tried ECHO but that doesn't work. script hanging
推荐答案
获取通过主机请求输入的 PowerShell 脚本的唯一方法 - 通常是通过 Read-Host
- 读取来自管道而不是键盘的输入用于将脚本作为外部PowerShell实例运行.
The only way to get PowerShell scripts that solicit input via the host - typically, via Read-Host
- to read input from the pipeline instead of from the keyboard is to use to run the script as an external PowerShell instance.
一个简单的例子:
# Does NOT work: pipeline input is IGNORED and Read-Host "hangs", i.e.,
# it waits for interactive input.
'y' | & { Read-Host "Y/N?" }
# OK: By launching the command via a new PowerShell instance,
# Read-Host reads from the pipeline (stdin).
# To run a script file externally, use -file instead of -command.
'y' | powershell -noprofile -command 'Read-Host "Y/N?"'
注意:通过外部 PowerShell 实例运行脚本/命令是有代价的:
Note: Running the script / command via an external PowerShell instance comes at a cost:
就性能而言,创建新的 PowerShell 进程成本高昂.
Creating a new PowerShell process is costly in terms of performance.
默认情况下,新实例的输出将是文本(字符串),而不是对象.
Output from the new instance will be textual (strings) by default, not objects.
- 您可以通过将
-OutputFormat xml
传递给外部实例并通过Import-CliXml
对结果进行后处理来近似通常的进程内体验代码>,但这有局限性.
- You can approximate the usual in-process experience by passing
-OutputFormat xml
to the external instance and post-processing the results viaImport-CliXml
, but that has limitations.
这篇关于自动执行 PowerShell 响应以提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!