Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2047
  • Last Modified:

Script needed to remove space in alias

Exchange 2007 no longer supports a space in the alias, so I need to remove it from about 200 DL's. I found the following article: http://www.exchangeninjas.com/RecipientValidation which provided me with the LDAP query to find them all, but now I need the code to remove the space.

I'm pretty new to Powershell, can anyone tell me the code for this?

 - Find all recipients with space/s in alias
 - Take results and remove space/s

thanks!
0
wallakyl
Asked:
wallakyl
  • 4
  • 3
1 Solution
 
BSonPoshCommented:
This should do what you want...

If you run it the first time and it does what you expect remove the # from the $user.SetInfo() line.
$filter = "(&(objectclass=*)(mailnickname=* *))"
$searcher = new-object DirectoryServices.DirectorySearcher([ADSI]"",$filter)
$searcher.pagesize = 1000
$users = $searcher.findall() | %{$_.GetDirectoryEntry()}
foreach($user in $users)
{
   Write-Host " + Processing User [$($user.DisplayName)]"
   $oldalias = $user.mailnickname[0] 
   Write-Host "   - Old Alias [$oldalias]"
   $newalias = $oldalias -replace "\s",""
   Write-host "   - New Alias to $newalias"
   $user.put("mailnickname",$newalias)
   #$user.SetInfo()
}

Open in new window

0
 
wallakylAuthor Commented:
BSonPosh,

Thanks for your help. Is there anyway to restrict this to just mail recipeints? Maybe with a Get-Recipient cmdlet? The reason I ask is because this query actually includes some of the default groups, like Offline Address Book and Schedule+ Free Busy Information. See screenshot below.

Thanks.
screenshot.JPG
0
 
BSonPoshCommented:
Try this... I think it will do it for you.

NOTE: same as before... remove the #
$Recipients = Get-Recipient -Filter "alias -like '* *'" -verb:$false
foreach($Recipient in $Recipients)
{
   $user = [ADSI]"LDAP://$($Recipient.DistinguishedName)"
   Write-Host " + Processing User [$($user.DisplayName)]"
   $oldalias = $user.mailnickname[0] 
   Write-Host "   - Old Alias [$oldalias]"
   $newalias = $oldalias -replace "\s",""
   Write-host "   - New Alias to $newalias"
   $user.put("mailnickname",$newalias)
   #$user.SetInfo()
}

Open in new window

0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
wallakylAuthor Commented:
Getting there :).

I am still seeing those built-in "users". When looking closer, it appears that they are all Public Folders. Do you know how to add a line that does all of the above, except for objects with type = Public Folder?

Thanks, I really appreciate the help.
0
 
BSonPoshCommented:
You can try this.. I do use public folders so I cannot test.

Notice the -filter parameter to Get-Recipient. That is what you need to play with


Param([Switch]$whatif)
$Recipients = Get-Recipient -Filter "(alias -like '*') -and (RecipientType -ne 'PublicFolder')" -result Unlimited
if(!$Recipients){Write-Host "No Recipients found with spaces";return}
 
foreach($Recipient in $Recipients)
{
   $user = [ADSI]"LDAP://$($Recipient.DistinguishedName)"
   Write-Host " + Processing User [$($user.DisplayName)]"
   $oldalias = $user.mailnickname[0] 
   Write-Host "   - Old Alias [$oldalias]"
   $newalias = $oldalias -replace "\s",""
   Write-host "   - New Alias to $newalias"
   $user.put("mailnickname",$newalias)
   if(!$whatif){$user.SetInfo()}
}

Open in new window

0
 
BSonPoshCommented:
btw... I made it into a script so you save it as a .ps1 file and pass -whatif to test.
0
 
wallakylAuthor Commented:
Thanks for the help. In the end, I ended up just manually changing them because I was spending more time trying to script it then it would take to manually do it. I appreciate the help though.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now