Hi.
I've just been struggling a bit with the Get-Mailbox cmdlet and been wondering if this is a general issue with filters in Powershell and why that even happens.
Import-Module ActiveDirectory | Out-Null
Add-PSSnapin "Microsoft.Exchange.Management.PowerShell.SnapIn"
$MyObject = ConvertFrom-JSON "{ Plan: '10GB', UPN: 'anzu@an.dev' }"
Get-Mailbox -Filter { CustomAttribute3 -eq $MyObject.Plan }
Now this actually gives me mailboxes which have CustomAttribute3 as $null.
Comparing it with the Get-ADUser cmdlet it becomes a bit more clear:
get-aduser -filter { UserPrincipalName -eq $MyObject.UPN }
get-aduser : Property: 'UPN' not found in object of type: 'System.Management.Automation.PSCustomObject'.
What also doesn't work is:
Get-ADUser -Filter { UserPrincipalName -eq "$($MyObject.UPN)" }
What of course works is just giving it a string property
$UPN = $MyObject.UPN;
Get-ADUser -Filter { UserPrincipalName -eq $UPN }
It doesn't appear to be a scriptblock issue either as I can run:
$myobject = ConvertFrom-json "{ Prop:'hihihi' }"
$scriptblock = { "hi $($myobject.prop)" }
& $scriptblock
Output: hi hihihi
Or just:
$scriptblockt ={ $myobject.prop }
& $scriptblockt
Output: hihihi
Someone can direct me to why this happens?