Hello Everyone,
I’m trying to cleanly match a list of users based on their primary SMTP address in ProxyAddresses, based on their domain names. The way I have it now works but I know it could be cleaner.
I have a text file with a list of domains; the script iterates through the list and finds the people.
The issue is I have to have every known permutation of capitalized or non-capitalized domain name because I’m using cmatch to grab the primary SMTP address from the list of proxy addresses.
Currently, I would have to have three separate entries in my text file to accommodate this situation.
## Go through each domain in the txt file called by $Domains ##
foreach ($Domain in $Domains) {
## Find only the users for this particular domain ##
$DomainUsers = $AllUsers | Where-Object {$_.ProxyAddresses -cmatch "^SMTP\:.*@$(($Domain))"} | Where-Object {$_.StrongAuthenticationRequirements.State -ne "Enforced"}
B U T :
Since $_.ProxyAddresses is an array you don’t get $true or $false as the return value when you use -match or -cmatch. Instead that works like a filter and you get the matching string as return value. It might work for you in this particular case because PowerShell considers everything else than a Zero as $true but I’m not sure if I’d go with this anyway.
If someone (accidently) saved it as name@DOMAIN.com or name@DOMAIN.COM for example you will not get the expected result.
I’d prefer to enrich the array of domain users I have with an additional calculated property containing the primary SMTP address of the user.
Thanks, and I believe that this would work if I were only working with a single domain. Our O365 tenant currently has over 550 domains and around 25,000 users. Also, because I’m working with MFA stuff, Get-MsolUser doesn’t have the luxury of a PrimarySMTPAddress Field, just ProxyAddresses.
In a perfect world, I would love to regex the match to always have capital SMTP and then be case insensitive to the $Domain variable.
An onPremisses AD does not have this either. That’s why I recommended to use a calculated property where you extract only the primary SMTP address from the property ProxyAddresses
That would enable you to use non case sensitive -match further on.