Proxyaddress search & match

I’ve been trying to write a PS script to do a search for matching proxy addresses and I’m running into trouble. The script has 2 criteria for matching:

Criteria 1 - Filter out users who have a primary e-mail address that matches a given value (“@contoso.com”, for instance).
Criteria 2 - For the remaining user list, use the primary e-mail address prefix (the part of the e-mail address that goes before the “@”) and see if that string exists in any of the user’s proxy addresses. If it does, don’t do anything. If there is no match, write out user info to a .csv file.

I’ve got criteria 1 working. The problem is with criteria 2. The list of proxy addresses is a string object type, but no matter what comparison operator I use, the output of the script is the entire list of filtered users, even those where the email prefix exists in the proxy address list. Here’s the code snippet:

Foreach($user in $users){
$mailprefix = $user.EmailAddress.Split("@")[0]
$proxyaddresses = $user.proxyaddresses
If(-not $proxyaddresses.Contains("$mailprefix")){
Out-File -FilePath C:\proxyaddresses.csv -Append
}
}

I’ve also tried “-notlike”, “-notcontains” and even “-notmatch”, even though I understand some of those only work with wildcard characters.

Thanks in advance for any assistance.

What does $proxyaddresses actually contain, for example? Is it a collection of string objects, or a single string with some kind of delimiter?

Hi, Don -

$proxyaddresses is a System.String object type that contains a list of e-mail addresses found in the user’s proxyaddress list. If I list the contents of the variable on the screen, I get:
SMTP:john.doe@contoso.com
SMTP:jdoe@acme.com
x.400:c=US;a= p=contoso;O=Exchange;s=doe,g=john

the only consistent delimiter would probably be a space.

What happens if you:

$proxyaddresses | measure

?

When I do a $proxyaddresses | Measure I get:

Count : 3
Average :
Sum :
Maximum :
Minimum :
Property :

Ok. So it isn’t a string, it’s a string - meaning, a collection, not a single object. Your code…

Foreach($user in $users){
$mailprefix = $user.EmailAddress.Split("@")[0]
$proxyaddresses = $user.proxyaddresses
If(-not $proxyaddresses.Contains("$mailprefix")){
Out-File -FilePath C:\proxyaddresses.csv -Append
}
}

Would work, because Contains() is a legit operation. The problem is that $mailprefix isn’t EXACTLY matching, because what’s in $proxyaddress also has SMTP: and the domain name in it. Contains() doesn’t do a wildcard match, it searches for one object within another. It’s the same as the -Contains operator and -eq or -like; see https://devopscollective.gitbooks.io/the-big-book-of-powershell-gotchas/content/manuscript/contains-isnt-like.html. I’ll also note that you weren’t telling Out-File to output anything ;).

We probably need to enumerate the proxy addresses.

Foreach($user in $users){
 $mailprefix = $user.EmailAddress.Split("@")[0]
 foreach ($addy in $user.proxyaddresses) {
  If(-not ($addy -like "$mailprefix")) {
   $addy | Out-File -FilePath C:\proxyaddresses.csv -Append
  }
}

Thanks, Don - I need to evaluate the proxyaddresses as a whole, not each individual one. The idea is that info gets written to a .csv file ONLY if the $mailprefix value is not present in ANY of the proxy addresses for the user. If it is, don’t do anything at all. That’s why I was hoping that the $proxyaddresses variable was a regular string object, so I could just do a substring match into 1 object instead of 3 or 4.

Foreach($user in $users){
 $mailprefix = $user.EmailAddress.Split("@")[0]
 $proxyaddresses = $user.proxyaddresses
 $combo = $proxyaddresses -join "|"
  If(-not ($combo -like "$mailprefix")) {
   $proxyaddresses | Out-File -FilePath C:\proxyaddresses.csv -Append
}

By combining the addresses into a pipe-delimited string, we can run -like against the entire set. But, $proxyaddresses remains a collection, so when piped to Out-File you’ll still get one per line.

Hi, Don -

Sorry for the delay. Out of the office for a while.

The comparison function doesn’t appear to be working. It writes user info to the .csv file even when there’s a match between the $combo & the $mailprefix value.

I like to use anr for proxies.

get-aduser -filter “anr -eq ‘SMTP:myemail@somewhere.com’”

Thanks, Dan - how would you use that in a comparison process? I need to compare proxy address values to a variable value.

criteria 1. get-aduser -Filter {anr -like “smtp:*@somewhere.com”}

criteria 2. no comparison needed. [bool](get-aduser -Filter {anr -like “smtp:first.last”})

(get-aduser -Filter {anr -like “smtp:danpotter”} -Properties proxyaddresses).proxyaddresses -cmatch ‘SMTP:’