如何在代码创建的 powershell shell 中执行脚本,其中包含 Write-Host 命令? [英] How can I execute scripts in a code created powershell shell that has Write-Host commands in it?

查看:26
本文介绍了如何在代码创建的 powershell shell 中执行脚本,其中包含 Write-Host 命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本,它依赖于导入模块中的函数.由于 IO(Web 请求),这个脚本需要一段时间,我想将它并行化为脚本块的数十万次迭代.

I have a script that I am writing which relies on functions in an imported module. This script takes a while due to IO (web requests) and I would like to parallize it for hundreds of thousands of iterations of a script block.

在尝试了几种不同的方法(由于 Start-Job 和其他东西的限制而收效甚微)之后,当前的实现依赖于预先创建一个通过 创建的 powershell shell"池$shell = [Powershell]::Create().

After attempting several different methods (with little success due to restrictions with Start-Job and other things) the current implementation relies on pre-creating a pool of powershell "shells" created via $shell = [Powershell]::Create().

我必须调用以引导外壳程序的模块方法之一(因此它处于正确状态)在其中调用了 Write-Host.当我调用 $shell.Invoke() 时发生以下错误:

One of the module methods I have to call to bootstrap the shell (so it's in a correct state) has a call to Write-Host in it. When I call $shell.Invoke() the following error occurs:

Write-Host : 提示用户失败的命令,因为宿主程序或命令类型不支持用户相互作用.尝试支持用户交互的主机程序,例如 Windows PowerShell 控制台或 WindowsPowerShell ISE,并从不支持用户交互的命令类型中删除与提示相关的命令,例如Windows PowerShell 工作流.

Write-Host : A command that prompts the user failed because the host program or the command type does not support user interaction. Try a host program that supports user interaction, such as the Windows PowerShell Console or Windows PowerShell ISE, and remove prompt-related commands from command types that do not support user interaction, such as Windows PowerShell workflows.

现在,由于模块是自定义的,我可以删除 Write-Host 调用,但是当它由最终用户直接运行时,这会降低用户友好性.如果参数为真,我可以创建一个不执行 Write-Host 的 switch 参数,但要这样做是一项很好的工作(可行,但我宁愿不这样做).

Now, since the module is custom I can remove the Write-Host calls, but that reduces user friendliness when it is run directly by end users. I can create a switch parameter that does not execute Write-Host if the parameter is true, but to do that down the line is a good bit of work (feasible, but I'd rather not).

有什么办法可以让 Write-Host 在这种情况下不出错?在这种情况下,我实际上并不关心输入,我只是不想要错误.

Is there any way I can get Write-Host to not error out in this scenario? I don't actually care about the input in this scenario, I just don't want the errors.

推荐答案

让它工作的最快方法是在你的脚本中定义一个虚拟的 write-host 函数,或者简单地定义它在运行脚本之前独立在运行空间中.

The fastest way to get this to work is to define a dummy write-host function in your script, or to simply define it in the runspace independently before running your script.

$ps.addscript("function write-host {}").invoke()
$ps.commands.clear()
# now you can invoke scripts that use write-host
# feel free to implement a write-host that writes to a log file

就这么简单.您收到该错误的原因是因为这样的程序调用不期望用户交互.有多种方法可以实现这一点,但它使用不同的 API.

Simple as that. The reason you're getting that error is because programmatic invocation like that does not expect user interaction. There are ways to make this work but it employs different APIs.

这篇关于如何在代码创建的 powershell shell 中执行脚本,其中包含 Write-Host 命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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