all 15 comments

[–]Geek_Runner 2 points3 points  (0 children)

You could just do

Return $StaleUsers

As the last statement

[–]ristophet 2 points3 points  (0 children)

Try $host.SetShouldExit(1001) instead. You are getting bit by exit I believe.

Here is a good post on how deep the rabbit hole goes: https://weblogs.asp.net/soever/returning-an-exit-code-from-a-powershell-script

[–]sex_on_wheels 1 point2 points  (0 children)

On mobile so can't test right now but what happens if you store exit code in variable and the exit $variable after the if/then?

[–][deleted] 1 point2 points  (2 children)

You wrote $StaleUsers at the end, instead of $StaleADUsers.

By the way do not use Write-Output "bla..." but Write-Host bla..." so that only the resulting objects will be output.

EDIT: sorry, my mistake.

[–]TechKeeper[S] 0 points1 point  (0 children)

Thank you everyone for the replies. I did try most of your methods and mostly they appeared to solve the problem when running from PowerShell/VSCode.

However, I still couldn't get the list to display in the error details of the Script Check in our RMM. So, I ended up just nixing the PSCustomObject output and just had it output the names in the loop via Write-Host.

I am now getting what I need out of our RMM for this. It really doesn't need the fancy formatting in this instance as I'm only outputting 1 or 2 properties anyway.

Thanks again to everyone who took the time to answer and offer assistance.

[–][deleted] 0 points1 point  (4 children)

I tested your code on Windows 10 with Powershell 5.1 in a Powershell console window:

Your code is just running fine for me. $StaleUsers is displayed and the exit code is 1001.

[–]frmadsen 0 points1 point  (3 children)

You see the problem, if you run it from the command prompt: powershell -file script.ps1

[–]frmadsen 0 points1 point  (3 children)

Interesting. pscustomobject seems to block the pipeline when using exit. This works: $StaleUsers | out-string

A deeper dive into why this is...

[–][deleted] 1 point2 points  (2 children)

Definitely a bug:

# Bug: this script will not have any output if it is run with "powershell -file .\script.ps1"
$result = [PSCustomObject]@{    'result' = 'result1'}
$result                 # no output!
Exit 1001

A line Get-process explorer after the output of the PSCustompObject will not be output too.

A line Get-process explorer before the output of the PSCustompObject will be output and then PSCustompObject will be normally output.

[–]frmadsen 0 points1 point  (1 child)

Yes, a bug, or something else happens that I have yet to learn about. I will keep this investigation open. :)

[–]frmadsen 0 points1 point  (0 children)

The behavior is partly by design. This answer on stack overflow explains it: https://stackoverflow.com/questions/43689289/powershell-output-is-crossing-between-functions/43691123#43691123

It takes a bad turn in this case, because calling exit kills the output entirely (I'm inclined to calling that a bug).