Creating a PowerShell script that calls an apps cmdlet. When then cmdlet runs it prompts for a response and the scripts hangs. The cmdlet doesn't have any parameters for this. Is there any way to respond programmatically? I tried ECHO but that doesn't work.
Asked
Active
Viewed 2,468 times
1
-
1Without seeing the `*assessment.ps1` script, unable to check if you can pass params to it. – Drew Nov 16 '18 at 02:37
1 Answers
4
The only way to get PowerShell scripts that solicit input via the host - typically, via Read-Host
- to read input from the pipeline instead of from the keyboard is to use to run the script as an external PowerShell instance.
A simple example:
# 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?"'
Note: Running the script / command via an external PowerShell instance comes at a cost:
Creating a new PowerShell process is costly in terms of performance.
Output from the new instance will be textual (strings) by default, not objects.
- You can approximate the usual in-process experience by passing
-OutputFormat xml
to the external instance and post-processing the results viaImport-CliXml
, but that has limitations.
- You can approximate the usual in-process experience by passing

mklement0
- 382,024
- 64
- 607
- 775