PowerShell, Regex, and Me; A Love Hate Relationship

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.

Is there a cleaner way to write the -cmatch regex below to account for examples like this:
SMTP:name@domain.com
SMTP:name@Domain.com
SMTP:name@doMain.com

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"}

Thanks for all the help,
Rob M

The proper pattern should be something like this:

'^SMTP:.*@(domain\.com|Domain\.com|doMain\.com)'

:+1:t4:

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. :thinking: :man_shrugging:t4:

If someone (accidently) saved it as name@DOMAIN.com or name@DOMAIN.COM for example you will not get the expected result. :point_up_2:t4: :wink:

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. :+1:t4: :slightly_smiling_face:

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. :dizzy_face: Also, because I’m working with MFA stuff, Get-MsolUser doesn’t have the luxury of a PrimarySMTPAddress Field, just ProxyAddresses. :melting_face:

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 :wink: :wink: :point_up_2:t4: :point_up_2:t4:

That would enable you to use non case sensitive -match further on.

I may have found a solution to both of our problems.

There’s a Regex switch for modifiers. For example, (?i) will turn on case insensitivity.
https://www.regular-expressions.info/modifiers.html

So in my snippet above - I modified it to look like this:

$AllUsers | Where-Object {$_.ProxyAddresses -cmatch "^SMTP\:.*@(?i)$(($Domain))"}|Sort DisplayName | Select ProxyAddresses

Now it will ignore the case for anything after the @ symbol. :heart_eyes:

Thanks,
Rob