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

Time:01-14

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}

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

CodePudding user response:

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
  • Related