Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I execute scripts in a code created powershell shell that has Write-Host commands in it?

Tags:

powershell

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.

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

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 : 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.

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

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.

like image 370
KallDrexx Avatar asked Jun 18 '14 16:06

KallDrexx


People also ask

What is write-host command used for in PowerShell?

Starting in Windows PowerShell 5.0, Write-Host is a wrapper for Write-Information This allows you to use Write-Host to emit output to the information stream. This enables the capture or suppression of data written using Write-Host while preserving backwards compatibility.

Does PowerShell allow you to run scripts that you have written on the local computer?

Before you can run a script on Windows, you need to change the default PowerShell execution policy. Execution policy does not apply to PowerShell running on non-Windows platforms. The default execution policy, Restricted , prevents all scripts from running, including scripts that you write on the local computer.


1 Answers

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.

like image 82
x0n Avatar answered Oct 23 '22 03:10

x0n