0
$roledetails = ./btp get security/role-collection `"$Role`" | Out-String

I have above command in PowerShell which executes the executable btp.exe using parameter provided. If it was successful, it provides "OK" and returns the results. I can capture result using $roledetails upon successful but not able to capture "OK" value which is printed on PpowerShell Command line.

If it is failure it prints "FAILED" value on screen and returns blank value in $roleDetails.

How do I capture "OK" OR "FAILED" system message ?

When it is

Lord OfTheRing
  • 1,097
  • 3
  • 12
  • 20
  • 2
    It's probably writing the status to `stderr`, which you can merge into the standard output stream like this: ```$roledetails = .{ ./btp get security/role-collection `"$Role`" } 2>&1 |Out-String```. Alternatively, check the value of `$LASTEXITCODE` immediately after running the executable. You'd expect `$LASTEXITCODE -eq 0` when successful, and a non-0 exit code on failure. – Mathias R. Jessen Oct 08 '21 at 13:38
  • @LordOfTheRing, note that you don't need `. { ... }` in order to apply a redirection such as `2>&1` Just to point it out: `Out-String` is only needed if you want to capture external-program output as a _single, multi-line string_ (regrettably, `Out-String` always appends a trailing newline - see [GitHub issue #14444](https://github.com/PowerShell/PowerShell/issues/14444)). PowerShell streams the output lines one by one, which means that if you capture them, you'll get an _array of lines_ (or a single string, if there happens to be just _one_ output line). – mklement0 Oct 08 '21 at 14:47
  • By default, only _stdout_ output (data) from external programs is captured / redirected / sent through the pipeline. To include _stderr_ output (errors, status information) i.e. to _merge_ the two streams, use `2>&1`. To selectively _suppress_ stderr, use `2>$null`. To _capture_ it - invariably in a _file_ - use `2>file.txt`. To determine if an external-program call _failed_, do not rely on stderr; instead, test if `$LASTEXITCODE` is nonzero. See [this answer](https://stackoverflow.com/a/57648600/45375) to the linked duplicate. – mklement0 Oct 08 '21 at 15:23

0 Answers0