Retrieve multiple proxy addresses

Import-Csv -Path “\server\input.csv” | ForEach-Object {
Get-ADUser -Filter "EmployeeID -eq '$($.EID)'" -Properties EmployeeID, proxyAddresses } |
Select-Object EmployeeID, SamAccountName, Enabled,
@{L = “ProxyAddresses”; E = { ($
.ProxyAddresses -like ‘@mycompany.org’ -or
($_.ProxyAddresses -notlike '
@mycompany.org’ -and
$_.ProxyAddresses -clike ‘SMTP:*’)) -join “;”}}

The script is working but I keep getting TRUE as the output not the proxy addresses.

I want to return all addresses with user@mydomain.org only. But there are also users that don’t have any of those and I want to return their primary SMTP address what ever it is :slight_smile: Thank you.

Hi, welcome to the forum :wave:

Firstly, when posting code in the forum, please can you use the preformatted text </> button. It really helps us with readability, and copying and pasting your code (we don’t have to faff about replacing curly quote marks to get things working).

How to format code on PowerShell.org

The comparison operators will return TRUE or FALSE you need to use them with Where-Object to filter results.

Your situation is a bit trickier because your pseudo code is:

if (the list of proxy addresses) -notcontains '*@mycompany.org'
return the primary SMTP address
else
return all addresses that match '*@mycompany.org'

However, in PowerShell we can’t use wildcards with the contains operators.

To work around this, we can join all the addresses and check the resulting string for a match. For readability, when declaring long calculated properties, I recommend assigning the hashtable to a variable and using the variable with Select-Object:

$proxyAddresses = @{
    label      = 'ProxyAddresses'
    expression = {
        if ($_.ProxyAddresses -join ',' -notmatch 'mycompany.org') {
            $_.ProxyAddresses | Where-Object { $_ -clike 'SMTP*' }
        }
        else {
            ($_.ProxyAddresses | Where-Object { $_ -like '*mycompany.org' }) -join ';'
        }
    }
}

Get-ADUser <code> | Select-Object <code>,$proxyAddresses
1 Like
if a users list of proxy addresses -contains '*@mydomain.org'
return ALL of those addresses ending in '*@mydomain.org' (There may be multiple, e.g. "Tom@mydomain.org" or "Tommy@mydomain.org" I would like to returrn all of those).
AND 
if a user doesn't have any '*@mydomain.org' addresses return their primary SMTP address what ever it is e.g. tom@domaintwo.org 
I am also for starter pulling from a CSV the EmployeeID for a specific group of users.


I know I am probably driving you crazy but let me try to illustrate this way. You have me so close. If a users list of proxy addresses -contains “@mydomain.org” return ALL of those addresses ending in “@mydomain.org” (There may be multiple, e.g. “Tom@mydomain.org” or “Tommy@mydomain.org” I would like to return all of those). AND if a user doesn’t have any " @mydomain.org" addresses return their primary SMTP address what ever it is e.g. tom@domaint_different.org

I am new at this and am trying to learn. I really appreciate your help and patience with my ignorance on formatting and syntax I will get there on my PowerShell journey.

Have you tried the code @matt-bloomfield shared? It actually does exactly what you’re looking for. :man_shrugging:t4:

1 Like

I did but I keep getting TRUE as the proxyaddresses. So your comment to use where each fixed that but the results are not what I needed. So close!!!

That’s impossible. Could you please show the exact code you used?

1 Like

All users have multiple email addresses ending in *@mydomain.org they also have other addresses with different variations and I want to return all of those addresses. e.g.

1@mydomain.org
2@mydomain.org

There are also users that don’t have any of those addresses and I want to return their Primary SMTP address e.g

1@different_domain.org

Import-Csv -Path "\\server\Input-Test.csv" | ForEach-Object {
 Get-ADUser -Filter "EmployeeID -eq '$($_.EID)'" -Properties EmployeeID, proxyAddresses |
  Select-Object EmployeeID, SamAccountName, Enabled, 
                  @{Name = 'ProxyAddresses'; Expression = { ($_.ProxyAddresses -like '*@mydomain.org') -join ';' }},
                  @{Name = 'PrimarySMTPAddress'; Expression = { $_.ProxyAddresses -notlike '*@mydomain.org' }} -ExcludeProperty ProxyAddresses | FT -AutoSize
}

So you decided not to use @matt-bloomfield 's suggestion?! … why not?

I’d recommend to give it a try:

$proxyAddresses = @{
    Name       = 'FilteredProxyAddresses'
    Expression = {
        if ($_.ProxyAddresses -join ',' -notmatch 'mydomain.org') {
            $_.ProxyAddresses | Where-Object { $_ -clike 'SMTP*' }
        }
        else {
            ($_.ProxyAddresses | Where-Object { $_ -like '*mydomain.org' }) -join ';'
        }
    }
}

Import-Csv -Path "\\server\Input-Test.csv" | 
ForEach-Object {
    Get-ADUser -Filter "EmployeeID -eq '$($_.EID)'" -Properties EmployeeID, proxyAddresses |
    Select-Object -ExcludeProperty ProxyAddresses -Property EmployeeID, SamAccountName, Enabled, 
    @{Name = 'ProxyAddresses'; Expression = { ($_.ProxyAddresses -like '*@mydomain.org') -join ';' } },
    @{Name = 'PrimarySMTPAddress'; Expression = { $_.ProxyAddresses -notlike '*@mydomain.org' } },
    $proxyAddresses
} |
Format-Table -AutoSize
2 Likes

I did use Matt’s and yours code and I just can’t get them to output what I need. The problem is me not being able to explain properly what I need and my lack of PowerShell experience.

This is the best I have been able to summarize it so far:

All users have multiple email addresses ending in *@mydomain_01.org or *@mydomain_02.org. I only want to return all of the *@mydomain_01.org

tom@mydomain_01.org
tommy@mydomain_01.org
thomas@mydomain_01.org
tom@mydomain_02.org
tommy@mydomain_02.org
thomas@mydomain_02.org

Then there are users that don’t have any of those addresses ending in *@mydomain_01.org or *mydomain_02.org and I want to return their primary addresses. e.g.

Tom@different_domain.org

You’ve explained it fine. You claim you tried Matt’s code and it returned true and that is simply incorrect. You should compare what you posted very closely to Matt’s suggestion. If his suggestion isn’t working 100%, then you should share what it did do.

You’re changing the requirements. Until now you’ve never mentioned 2 different domains yet. :face_with_raised_eyebrow:

Anyway the code I posted should at least provide the results for one desired domain. If you still get True instead of the desired output you’re not using the code we suggested.

1 Like

Please don’t post code as an image. Use the method detailed in my first reply.

The code I posted gives the correct results; this is it incorporated with your code:

$proxyAddresses = @{
    label      = 'ProxyAddresses'
    expression = {
        if ($_.ProxyAddresses -join ',' -notmatch 'ford.org') {
            $_.ProxyAddresses | Where-Object { $_ -clike 'SMTP*' }
        }
        else {
            ($_.ProxyAddresses | Where-Object { $_ -like '*ford.org' }) -join ';'
        }
    }
}

Import-Csv -Path '\\SERVER\input.csv' | ForEach-Object {
    Get-ADUser -Filter "EmployeeID -eq '$($_.EID)'" -Properties EmployeeID,ProxyAddresses | 
        Select-Object EmployeeID,SAMAccountName,Enabled,$proxyAddresses |
            Export-CSV \\SERVER\export.csv -Append -NoTypeInformation
}

Looks OK to me, but I can’t test it right now so might have missed a typo or two.

1 Like

Its working but it still does not include any other proxy addresses I would like. I know this is really hard to explain. Can we try a different way? How about we say return all proxy addresses except this one, and this one, and this one. That way I will get all proxy addresses and it is up to me to exclude the ones I don’t want.

In this case you simply use a Where-Object but with negative comparison operators like -notmatch or -notlike -or -notcontains or -notin and so on …

1 Like