Author Posts

June 22, 2016 at 1:51 pm

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.

June 24, 2016 at 4:40 pm

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

June 27, 2016 at 7:41 pm

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.

June 27, 2016 at 7:50 pm

What happens if you:

$proxyaddresses | measure

?

June 28, 2016 at 6:25 pm

When I do a $proxyaddresses | Measure I get:

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

June 28, 2016 at 6:37 pm

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

June 29, 2016 at 7:28 pm

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.

June 30, 2016 at 2:16 pm

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.

July 12, 2016 at 2:28 pm

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.

July 12, 2016 at 3:04 pm

I like to use anr for proxies.

get-aduser -filter "anr -eq 'SMTP:myemail@somewhere.com'"

July 12, 2016 at 6:10 pm

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

July 12, 2016 at 6:34 pm

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:'

  • This reply was modified 2 years, 2 months ago by  Dan Potter.