Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why can't I see the output of another executable in an invoked ScriptBlock?

I am creating and invoking PowerShell ScriptBlock objects. I noticed that when the ScriptBlock includes another executable, I am not able to see the output from that process. Keyboard input is still accepted though.

Here is a simplified version of the problem. In this case, I do not see any output from cmd.exe but I can type ‘exit{ENTER}’ and return to PowerShell

function Test-CMD {cmd.exe}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$sb = [System.Management.Automation.ScriptBlock]::Create('Test-CMD')

$sb.Invoke()

Is there a way to get the executable output to the console? Other ScriptBlock output works as expected.

>Solution :

PowerShell is blocked until the method ($sb.Invoke()) returns – to avoid this, use the & call operator instead:

function Test-CMD {cmd.exe}

$sb = [System.Management.Automation.ScriptBlock]::Create('Test-CMD')

& $sb
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading