如何在代码中创建具有Write-Host命令的powershell shell中的脚本?

时间:2023-01-20 21:30:43

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.

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

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().

在尝试了几种不同的方法(由于Start-Job和其他东西的限制而几乎没有成功)之后,当前的实现依赖于预创建通过$ shell = [Powershell] :: Create()创建的powershell“shell”池。

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:

我必须调用一个模块方法来引导shell(因此它处于正确状态),其中有一个Write-Host调用。当我调用$ shell.Invoke()时,会发生以下错误:

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:一个命令,提示用户失败,因为主机程序或命令类型不支持用户交互。尝试支持用户交互的主机程序(如Windows PowerShell控制台或Windows PowerShell ISE),并从不支持用户交互的命令类型(如Windows PowerShell工作流)中删除与提示相关的命令。

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调用,但是当最终用户直接运行时,这会降低用户友好性。如果参数为true,我可以创建一个不执行Write-Host的switch参数,但要做到这一点是很好的工作(可行,但我宁愿不这样做)。

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不出错?我实际上并不关心这种情况下的输入,我只是不想要错误。

2 个解决方案

#1


17  

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

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.

就那么简单。您收到该错误的原因是因为程序化调用不期望用户交互。有一些方法可以使这项工作,但它采用不同的API。

#2


6  

If you need the output, you can use:

如果需要输出,可以使用:

$ps.addscript("function write-host($out) {write-output $out}").invoke()
$ps.commands.clear()

#1


17  

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

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.

就那么简单。您收到该错误的原因是因为程序化调用不期望用户交互。有一些方法可以使这项工作,但它采用不同的API。

#2


6  

If you need the output, you can use:

如果需要输出,可以使用:

$ps.addscript("function write-host($out) {write-output $out}").invoke()
$ps.commands.clear()