Generating unique email address

So my colleague Rene van Amersfoort walks by, “Hey mr PowerShell!” Stop… Mr. PowerShell is Jeffrey Snover, I’m just a humble PowerShell enthusiast, but keep going, I’m listening…

“So I want to generate some unique email address for this office 365 migration I’m doing. The naming convention is as follows: “First letter GivenName””SurName without spaces”
I’ve got a bit of a challenge, what if the email address is already used? Then I need to increment the email address with 1 and so forth. Care to take a look at what I’ve got so far?”

“Do I??? Is a duck’s butt waterproof???” (Hey, doesn’t take much to get me to look at a PowerShell script, plus the Mr PowerShell was a nice comment, how can I say no eh? 🙂 ) Off we go!!!

Ok, here in the Netherlands SurNames are notorious for having prefixes like ‘van’ and ’van der’. There is no Active Directory attribute for this so it ends up somewhere in the sn or in the DisplayName property.

Let’s see what you got so far… Egad! What did puppies ever do to you? “Huh? (Poor Rene is confused)” Five innocent puppies, I hope you’re happy… (Nope, Rene is still clueless). CBT nuggets followers get it, “Everytime someone uses Write-Host a puppy dies”, the horror. Oh but it gets better, according to Don Jones “there’s a special place in Hell reserved for those that use Write-Host…” I’ll spare poor Rene eternal torture…

Say, why don’t we just gather the information in a [PSCustomObject] ok? That way we can evaluate later on. “Eh [PSCustomObjects]?” Ok, seems I got my work cut out for me. So I started telling Rene about the advantages of using objects. In hindsight, his bewildered look should have told me to take it easy, sorry about that Rene, it’s the PowerShell force…

You know what? Don’t sweat it. This was a learning moment for me personally. Hey I’m ready to make a believer of anyone if they give me a chance, but you have to pace yourself, bring yourself to their level or you’ll lose them.

Ok, let’s collect all known user name and mail property first for reference.

$colUserMail = get-aduser -Filter * -Properties mail | select name,mail

Do you have a CSV file with the users you want to process? Nice!!!
Let’s import them

$users = import-csv -path C:\Migration_Script\CSV\_BATCH\_newaccounts\New-IDs.csv -Delimiter ','

Ok so you have the foreach that’s good. For each user in the CSV File we need the following properties : Name,GivenName, sn, DisplayName

$Name = Get-ADuser -Identity $account -Properties name,givenname,sn,displayname

Let go ahead and create an empty to collect our objects:

$arrResults = @()

Remember the syntax?

$s = $Name.givenname
$t = $s.Substring(0,1) #This will give you the first letter
$surename = ($name.sn -split ' ') -join '' #This will get rid of any spaces in SurNames with prefixes.

Incidentally I recently learned about $OFS (Object field Separator), we’ll just stick with this.

Your new email address is:
$mail = $t+$surename+”@newdomain.com”. At this point I could have stated that using ‘+’ isn’t really the way to manipulate strings, but I’ve also been there. The objective now is to help Rene with the script the way he feels comfortable.

We also have to set the targetAddress and ProxyAddress

$targetAddress = "smtp:"+$account+"@newdomain.com"
$ProxyAddress = "SMTP:"+$t+$surename+"@newdomain.com"

So now it’s time to verify if your new email address already exists

#Verify if user email exists
if (!$colUserMail.mail.Contains($mail)) {
   $newEmail = $mail
}
Else{
   $i=1
   Do {
      $newEmail = $t+$surename+"$i@newdomain.com"
      $i++
   } While (!$colUserMail.mail.Contains($mail))
}

let’s just gather that:

$hshUserMail = [PSCustomObject]@{
   SamAccountName = $user.Account
   Email = $newEmail
   TargetAddress = $targetAddress
   ProxyAddress = "smtp:$newEmail"
}

add to the $arrResults we created and we’re good!

$arrResults += $hshUserMail

Ok let’s run this and see what we got!

$arrResults | ogv

“Say what’s ogv?” oh that the alias for out-gridview, so yeah out-gridview is really cool you can… (Urv, contain yourself!!!)

We gave a test user a known email address a ran it and sure enough “userx1@newdomain.com” was returned.

Ok, so you got from here? “Yep, sure do! Thanks mate!!!” No, thank you for letting me show you how awesome PowerShell is!!! :-p

So Rene is happy and I learned a valuable lesson in not overrunning my colleagues with too much information. Let them get there at there own pace. By all means extend that helping hand, but make sure it does more good than harm…

Ttyl,

Urv

Advertisement

1 thought on “Generating unique email address

  1. Pingback: Move and disable AD users past expiration date | pshirwin

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s