r/sysadmin • u/TheBlueFireKing Jack of All Trades • Apr 30 '26
PSI: Using $Test in ExchangeOnline PowerShell Scripts
In the last days we suddenly had multiple scripts fail with the following Error:
Cannot convert value "System.Management.Automation.PSCustomObject" to type "System.Boolean". Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or O. (Cannot convert value "System.Management.Automation.PSCustomObject" to type "System.Boolean". Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or O. (Cannot convert value
"System.Management.Automation.PSCustomObject" to type "System.Boolean". Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or O.))There were no changes to the Script itself. We are running them over AzureAutomate on Hybrid Workers. Up to date ExchangeOnline Module running Windows Powershell.
Now we do have the following parameter on most scripts so we can run / test them without it doing modifications:
param
(
[Parameter(Mandatory = $False)]
[Bool] $Test = $False
)Now it seems like Microsoft push some kind of change that the Get-Mailbox cmdlet now internally sets the variable $Test to something else which triggers the error. Why the f $Test would be set in a Prod environment is beyond me.
We changed all $Test to another variable name and everything is running fine again.
Just dropping this here if someone else runs into this problem ...
35
u/omglazrgunpewpew Apr 30 '26
Nice catch. Exactly the kind of change that eats half a day because nothing in your script changed, yet suddenly PS is deadpan insisting PSCustomObject is a boolean now.
I’ve started avoiding names like $Test, $Mode, $Action, $Debug, etc. in automation for this reason. Too many modules, proxy functions, hidden scopes, generated commands, and mysterious Microsoft “quirks” all swirling about in the same session.
ExchangeOnline PS does sometimes feel like, “we changed something behind the curtain, enjoy your morning!”
Thanks for posting the fix. May it spare someone else from the sacred ritual of angrily adding
Write-Hosteverywhere.5
u/Key-Level-4072 Apr 30 '26
Did you not do exactly that?
4
u/Frothyleet Apr 30 '26
Couple of notes:
Setting that aside, the way you are trying to use that "$Test" parameter in your script, you can replace everything in that param() block with:
[switch]$Test
Switches are never mandatory and will default to $false, unless the parameter is called (without any arguments). Your way technically works but again you are reinventing something powershell does already.
21
u/purplemonkeymad Apr 30 '26
Looking more it appears that it might be a debugging result that was left in. The culprit is the function PrintResultAndCheckForNextPage which has this at the top:
Looks like someone was fixing this and left in the debug info.
3
u/purplemonkeymad Apr 30 '26
Looks like it's the graph api result. Also funny to see that the exchange team must just use the beta endpoint in prod for the same reason i see other people doing it.
Also happens for other exchange commands eg Get-DistributionGroup.
3
u/justaguyonthebus Apr 30 '26
Oh, this is bad. I just put the pieces together. The OP got lucky in that PSCustomObject doesn't convert to Boolean in PS5 (and I think PS7 fixed that). That's why this thankfully blew up instead of evaluating true in all their logic and flipping production into test mode silently.
The silent failure would have been a nightmare to solve even for the best of us.
2
u/BlackV I have opnions Apr 30 '26 edited May 01 '26
Change this
To
Then use
.ispresentand your$PSBoundParametersvariableOr properly implement
-whatifwith advanced function (although that can cause issues with external modules)Edit: saw your note about azure automation, til that that would bite me
1
u/justaguyonthebus Apr 30 '26
This is a scoping issue and exactly why global scoped variables are shunned. MS should have scoped it differently. But it's a good practice to initiate variables with initial conditions before you use them, and that's what your fix does.
1
u/fosf0r Broken SPF record May 01 '26
Laughs in
$matches =