Hi there,
I have a script which we made to remove an erroneous proxy address that was initially made in error (a typo)
import-module activedirectory $cred = Get-Credential $users = get-aduser -searchbase "OU=Users,OU=example,OU=campus,DC=domain,DC=internal" -filter{pager -eq "Staff" -and enabled -ne $false -and proxyaddresses -like "*@student.example.ac.uk"} -properties displayname, UserPrincipalName, proxyaddresses, mailnickname, mail #| select displayname, UserPrincipalName, proxyaddresses, mailnickname, mail foreach ($user in $users) { $name = $user.SamAccountName $UPN = $user.userprincipalname $removeproxy = 'smtp:'+ $name + '*@student.example.ac.uk' -replace " ","." # $removeproxy2 = 'smtp:'+ $UPN # $replaceproxy = 'smtp:'+ $user.displayname + '@otherexample.ac.uk' -replace " ","." # $newproxy = 'SMTP:'+ $UPN # $newmail = $UPN # $student = 'smtp' + $name + '@student.otherexample.ac.uk' if ($user.proxyaddresses -eq $removeproxy) { write-output "proxyaddress present for $name changing data" | out-file "c:\Success_change_proxy_data.txt" -append set-aduser -identity $name -remove @{proxyaddresses=$removeproxy} -WhatIf #set-aduser -identity $name -add @{proxyaddresses=$replaceproxy,$newproxy;mail=$newmail} } else {write-output "Proxyaddress not present for $name no change" | out-file "c:\Error_change_proxy_data.txt" -append}
At the moment, it searches for people with the provided searchbase and removes an smtp address which matches the above query.
We have another smtp address which we would like to remove, however, due to these members of staff having had namechanges in the past. Their SamAccountName no longer matches their proxy address.
It might all sound confusing, but all i really need to do, is edit the $removeproxy variable, to accept a wildcard, e.g
‘smtp’ +‘*@student.example.ac.uk’
I know this is simple, but im having a very bad mental block at the moment!