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

后端 未结 3 948
野性不改
野性不改 2021-01-08 00:06

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 h

相关标签:
3条回答
  • 2021-01-08 00:45

    Try changing your WMF version to 5.1 - https://www.microsoft.com/en-us/download/details.aspx?id=54616

    0 讨论(0)
  • 2021-01-08 00:52

    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.

    0 讨论(0)
  • 2021-01-08 00:53

    If you need the output, you can use:

    $ps.addscript("function write-host($out) {write-output $out}").invoke()
    $ps.commands.clear()
    
    0 讨论(0)
提交回复
热议问题