Retrieve multiple proxy addresses

Can you help me plug that into the code we are using? I keep plugging in the Where-object -notlike multiple times for each addresses I want to filter out and am getting no where, help!

I don’t understand what you want. Please post an example with a clear explanation. None of your replies so far are giving us any clue as to what’s wrong so I suggest you post a list of example addresses and what you expect the results to be.

The code I’ve posted, as far as I’m concerned, filters it as you have described.

Here’s the sample I used for testing and its output:

# Sample Data for Testing
$users = @(
    @{
        ProxyAddresses = @(
            'SMTP:buffy@anotherdomain.org'
            'smtp:buffy@domain.org'
            'smtp:buffy@differentdomain.org'
        )
        EmployeeID = '123'
        SAMAccountName = 'buffys'
        Enabled = $true
    }
    @{
        ProxyAddresses = @(
            'SMTP:willow@mydomain.org'
            'smtp:willow@domain.org'
            'smtp:willow.rosenberg@mydomain.org'
        )
        EmployeeID = '456'
        SAMAccountName = 'willowr'
        Enabled = $true
    }
)

# Calculated Property

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

# Simulate Getting Users from AD, and Selecting Properties

$users | Select-Object EmployeeID,SAMAccountName,Enabled,$proxyAddresses

# Output

EmployeeID SAMAccountName Enabled ProxyAddresses
---------- -------------- ------- --------------
123        buffys            True SMTP:buffy@anotherdomain.org
456        willowr           True SMTP:willow@mydomain.org;smtp:willow.rosenberg@mydomain.org
2 Likes

I am sorry this is turning out like this. My desired output has never changed and I only asked for a different approach on last post to see if that would make it easier. I am not able to convey the need properly and I am trying. Your code works beautifully but it is missing one part that I obviously can’t comminute correctly. Let me try one more time and then I will quit bugging you.

I want to retrieve all addresses from the CSV of users by employeeID that end in @ford.org only (working great). However there are users that don’t have @ford.org and I want to pull their SMTP address that is different.

But that’s exactly what my code does. Let me change the test domain names and add additional comments. If the output is incorrect, based on these users’ proxy addresses, what would you expect it to be?

# Sample Data for Testing
$users = @(
    @{
        # No ford.org addresses, the result for this user will be the primary SMTP address.
        ProxyAddresses = @(
            'SMTP:buffy@renault.org'
            'smtp:buffy@renaultcars.org'
            'smtp:buffy@renaultmotors.org'
        )
        EmployeeID = '123'
        SAMAccountName = 'buffys'
        Enabled = $true
    }
    @{
        ProxyAddresses = @(
        # Two ford.org addresses, these will be joined by a semi-colon ';'
            'SMTP:willow@renault.org'
            'smtp:willow@ford.org'
            'smtp:willow.rosenberg@ford.org'
        )
        EmployeeID = '456'
        SAMAccountName = 'willowr'
        Enabled = $true
    }
)

# Calculated Property

$proxyAddresses = @{
    label      = 'ProxyAddresses'
    expression = {
        # If none of the proxy addresses are @ford.org, pull the SMTP address.
        if ($_.ProxyAddresses -join ',' -notmatch 'ford.org') {
            $_.ProxyAddresses | Where-Object { $_ -clike 'SMTP*' }
        }
        # Else return all ford.org addresses.
        else {
            ($_.ProxyAddresses | Where-Object { $_ -like '*ford.org' }) -join ';'
        }
    }
}

# Simulate Getting Users from AD, and Selecting Properties

$users | Select-Object EmployeeID,SAMAccountName,Enabled,$proxyAddresses

Output:

EmployeeID SAMAccountName Enabled ProxyAddresses
---------- -------------- ------- --------------
123        buffys            True SMTP:buffy@renault.org
456        willowr           True smtp:willow@ford.org;smtp:willow.rosenberg@ford.org

This is the part I am adding to the end to parse through the users by EmployeeID. Please don’t get mad at me I have included this from the beginning before. You are so close. I’ll really, really appreciate your help!

Import-Csv “\server\Input.csv” |
Foreach {Get-ADUser -Filter “EmployeeID -eq ‘$($_.EID)’” -Properties EmployeeID, SamAccountName, Enabled, proxyAddresses } | Select-Object EmployeeID, Enabled, $ProxyAddresses

I am gong to respectfully bow out. I have wasted enough of your time. I have not been able to explain the return I want and it is not your fault in anyway. Thank you for everything. I appreciate your help.

You should not give up until we give up!!! :wink: :love_you_gesture:t4: … we’re still with you.

Could you please share an example of maybe 2 or more different users with ALL their proxyaddresses and the expected output for all of them?

1 Like

I actually got it to work by adding two more lines. I know not good scripting technique. :slight_smile:

$proxies2 = @($user.ProxyAddresses | Where-Object { $_ -like ‘@myaddresss.org’ -and ($_ -clike ‘smtp:*’) })

and OtherProxyAddresses = $proxies2 -join '; ’

But now I get the header to return on each output but the output is what I needed. :smile:

This is what I am using.

Import-Csv -Path “\server\Input.csv” | ForEach-Object {
$user = Get-ADUser -Filter "EmployeeID -eq '$($.EID)'" -Properties EmployeeID, proxyAddresses, SamAccountName, Enabled
if ($user) {
$proxies = @($user.ProxyAddresses | Where-Object { $
-like ‘@mycompany.org’ })
$proxies2 = @($user.ProxyAddresses | Where-Object { $_ -like ‘@mycompany.org’ -and ($_ -clike ‘smtp:*’) })
if ($proxies.Count -eq 0) { $proxies = $user.ProxyAddresses | Where-Object { $_ -clike ‘SMTP:*’ }
[PsCustomObject]@{
EmployeeID = $user.EmployeeID
SamAccountName = $user.SamAccountName
Enabled = $user.Enabled
DefaultProxyAddress = $proxies
OtherProxyAddresses = $proxies2 -join '; ’
} | ft
}
else {
Write-Warning “User with EmployeeID ‘$($_.EID)’ not found…”
}
} }

Before I go to bed … remove the “| ft” from your code! :wink:

LOL. I will. Thank you.