gpt4 book ai didi

powershell - 自动执行PowerShell响应以提示

转载 作者:行者123 更新时间:2023-12-03 01:02:57 25 4
gpt4 key购买 nike

创建调用应用程序cmdlet的PowerShell脚本。然后,在cmdlet运行时,它会提示您进行响应,并且脚本会挂起。该cmdlet没有任何参数。有什么方法可以通过程序进行响应吗?我尝试了ECHO,但是那没用。
script hanging

最佳答案

获取通过主机(通常通过Read-Host)请求输入的PowerShell脚本到而不是从键盘读取管道中的输入的唯一方法是使用作为外部PowerShell实例来运行脚本。

一个简单的例子:

# 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实例运行脚本/命令需要付费:
  • 就性能而言,创建新的PowerShell进程成本很高。
  • 默认情况下,新实例的输出将是文本(字符串),而不是对象。
  • 您可以通过将-OutputFormat xml传递给外部实例并通过Import-CliXml对结果进行后处理来近似通常的过程中体验,但这有其局限性。
  • 关于powershell - 自动执行PowerShell响应以提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53330644/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com