Hi all,

I need to run a powershell script against nearly 4000 user accounts which will meet the following conditions:

  1. Search user proxyAddress for SMTP attribute

  2. IF SMTP proxyAddress = “*@domain.org” then add an alias smtp proxyAddress =“smtp:”+$user.sAMAccountName+"@domain.net"

This is what I’m working off so far… it doesn’t generate any errors for me, but does not complete the task as required.

Any assistance would be greatly appreciated!

$userou = “OU=Ontario,DC=domain,DC=local”
$users = Get-ADUser -Filter * -SearchBase $userou -Properties SamAccountName,ProxyAddresses

Foreach ($user in $users)

{

If ($PrimarySMTP -like “*@domain.org”)
{Set-ADUser -Identity $user.samaccountname -Add @{proxyAddresses=“smtp:“+$user.sAMAccountName+”@domain.net”}}

}

  • I’m sure " If ($PrimarySMTP -like “*@domain.org”)" should have the notation $True in there somewhere, but I’m not sure how to apply it.
6 Spice ups

Your script never defines a value $PrimarySMTP, so Powershell will see it as null. You can do something like this:

$userou = "OU=Ontario,DC=domain,DC=local"
$users = Get-ADUser -Filter * -SearchBase $userou -Properties SamAccountName,ProxyAddresses 

Foreach ($user in $users)

{

If ($User.ProxyAddresses -match "*@domain.org") 
{Set-ADUser -Identity $user.samaccountname -Add @{proxyAddresses="smtp:"+$user.sAMAccountName+"@domain.net"}}

}
1 Spice up

Not quite. The conditions in If statements evaluate to $True or $False. If the condition equals $True, then the command block within the If statement will run. Your condition does not need to contain $True. You need it to be a true statement.

For example, you can check if a variable exists (has non-null value):

PS > $test = "test"

PS > If ($test) {"True"} else {"False"}
True

PS > $test = $null

PS > If ($test) {"True"} else {"False"}
False
1 Spice up

proxyaddresses will return an array of addresses. You’ll have to do something like if ($user.proxyaddresses -contains “SMTP:”){add the new smtp value}

1 Spice up
Here's some code that I wrote that checks to see if a user has the correct SMTP address
$details = get-aduser $domainuser -properties proxyaddresses
### Initialize a blank array
$array = @()
### Step through the returned proxyaddresses and put them in a new array that we can mess with
foreach ($proxy in $details.proxyaddresses) {
    $array += $proxy
}
### Step through our new array and check if the user already has a primary SMTP (designated by being all CAPS)
foreach ($proxy in $array) {
    $first, $last = $proxy.split("@")
    $first, $second = $first.split(":")
    if ($first -ceq "SMTP:" -And $last -eq "mydomain.org") {
        write-host $domainuser "already has the correct primary proxy address AD attribute. No changes necessary"
        $emailbody += "<br>" + $username + " already has the correct SMTP address"
    }
    if ($first -ceq "SMTP") {
        write-host "Found invalid primary proxyaddress. Converting the wrong proxyaddress and adding the correct one"
        $emailbody += "<br>Found invalid primary proxyaddress " + $first +  "Converting the wrong proxyaddress and adding the correct one"
        Set-Aduser -identity $domainuser -Remove @{ProxyAddresses = $proxy.ToString()}
        $correctaddress = "SMTP:" + $domainuser + "@mydomain.org"
        $replaceaddress = "smtp:" + $proxy.split(":")[1]
        Set-Aduser -identity $domainuser -Add @{ProxyAddresses = $correctaddress}
        Set-Aduser -identity $domainuser -Add @{ProxyAddresses = $replaceaddress}
    }
}

Welcome OP,

If you post code, please use the ‘Insert Code’ button. Please and thank you!
PLEASE READ BEFORE POSTING! Read if you're new to the PowerShell forum! .

codebutton_small.png

@simonfanning3565 @stevenpeterson1053

1 Spice up

If you get the desired output with

 Get-ADUser -Filter "ProxyAddresses -like '*@domain.org'" -SearchBase "OU=Ontario,DC=domain,DC=local" -Properties ProxyAddresses|select Name,proxyaddress

than this should work

 Get-ADUser -Filter "ProxyAddresses -like '*@domain.org'" -SearchBase "OU=Ontario,DC=domain,DC=local" -Properties ProxyAddresses |Foreach{
$smtp="smtp:"+$_.samaccountname + "@domain.net"
Set-ADUser -Identity $_.samaccountname -Add @{proxyAddresses=$SMTP}
}

Not only short and sweet, but the perfect solution to my issue!!

Thank you so very much for your help!

@jitensh

Please mark the answer as best answer and close the topic

#close