INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Convert email address template string to valid email address

Convert email address template string to valid email address

Convert email address template string to valid email address

(OP)
Hello,
I'm creating a script that will validate new email addresses, based on our email address policy, to make sure they are not already in use. However, I'm stuck trying to convert the email address template strings into valid email addresses so they can be verified as useable?

Here's an example: I have a new user named John Doe (samaccountname: jdoe12345). According to our email address policy John will get the email addresses of %g.%s@mydomain.com (John.Doe@mydomain.com) and %m@mydomain.com (jdoe12345@mydomain.com). I need to verify those addresses are not used before I create the account.

I hate to hard code anything if I don't have to, but would it be easiest to just hard code a variable conversion? Something like: search for %g in the address string and then replace it with the first name.

Thanks.


Light travels faster than sound. That's why some people appear bright until you hear them speak.

RE: Convert email address template string to valid email address

Depends on how you want to input data. Do you want to do something like

CODE --> PowerShell

Check4Duplicate -FirstName "John" -LastName "Doe" 
Because that would be easy enough to just build the potential email address ($FirstName + "." + $LastName + "@contoso.com") and then query AD to see if it's already there.

Do you have your Tek-Tips.com Swag? I've got mine!

Stop by the new Tek-Tips group at LinkedIn.

RE: Convert email address template string to valid email address

(OP)
58Sniper,
First off, thank you for your reply.

In a sense that is what I'm doing. However, we have several email domains as well as several different ways of addressing those domains (ie FirstName.LastName@mydomain.com, FirstName.LastName@anotherdomain.com, SAMAccountName@mydomain.com, SAMAccountName@thirddomain.com, etc.).

I figured the best way to handle the addresses was to dynamically pull the email address template strings from our email policy, convert them to valid addresses, and then check for duplicates. This way if domains are added/removed, I don't have to worry about whether or not I'm notified. Plus, I don't have to go back to my script and modify any hard-coded domains or variable conversions.

My script is complete, with one exception. I'm having trouble translating the variables in the address template string. That section is hard-coded to search for the specific variables (%g, %s, and %m) and convert them (FirstName, LastName, SAMAccountName). I would like to make that dynamic.

Maybe the code will help. (I'm still pretty new to PowerShell script writing so if you see something that can be improved I'm open to ideas.)

CODE

Function PPC-Validate-Unique-Email-Addresses{
	 param($fname, $lname, $samaccountname)

	 $gname = "%g"
	 $sname = "%s"
	 $sam = "%m"
	 $valid_addresses = $TRUE
	 
	 ## "New" is the name of the current emailaddresspolicy
	 $epolicy = Get-EmailAddressPolicy -Identity "New"
	 
	 ## Extract email address templates from the policy
	 $address_strings = $epolicy.EnabledEmailAddressTemplates | foreach {$_.AddressTemplateString}
	 
	 write-host "";""
	 write-host "Acquiring user address list..." -foregroundcolor "yellow"
	 $all_mailboxes = Get-Mailbox -ResultSize unlimited | select -expand EmailAddresses | %{$_.SmtpAddress}
	 
	 write-host "Acquiring distribution group address list..." -foregroundcolor "yellow"
	 $all_groups = Get-DistributionGroup -ResultSize unlimited | select -expand EmailAddresses | %{$_.SmtpAddress}
	 
	 write-host "Acquiring contact address list..." -foregroundcolor "yellow"
	 write-host "";""
	 $all_contacts = Get-MailContact -ResultSize unlimited | select -ExpandProperty EmailAddresses | %{$_.SmtpAddress}
	 
	 ## Combine all the lists
	 $all_addresses = ($all_mailboxes + $all_groups + $all_contacts)

	 ## Convert each template into a valid email address, then check if that address already exists
	 foreach ($add_str in $address_strings)
		{
		 $email_found = $FALSE
		 ## Convert template to valid address
		 $new_email = $add_str
		 if ($new_email -match $gname)
			{$new_email = $new_email -replace $gname, $fname}
		 if ($new_email -match $sname)
			{$new_email = $new_email -replace $sname, $lname}
		 if ($new_email -match $sam)
			{$new_email = $new_email -replace $sam, $samaccountname}
			
		 write-host "Verfying $new_email...." -nonewline
		 if ($all_addresses -contains $new_email)
			{
			 $email_found = $TRUE
			 $dup_user = get-recipient -resultsize unlimited | where {$_.EmailAddresses -match $new_email}
			 write-host ""
			 write-host "$new_email is already assigned to:" $dup_user[0].name -foregroundcolor "red"
			 $valid_addresses = $FALSE
			}
		 if(!$email_found)
			{write-host "VERIFIED!" -foregroundcolor "yellow"}
		 write-host "";""
		}
	 return $valid_addresses
	}

## The function would be called like this:
$fname = "John"
$lname = "Doe"
$samaccountname = "jdoe12345"
[string]$unique_email = PPC-Validate-Unique-Email-Addresses $fname $lname $samaccountname 
$unique_email = $unique_email.trim()
if ($unique_email -eq $TRUE)
  {## Enable the mailbox} 

Thanks


Light travels faster than sound. That's why some people appear bright until you hear them speak.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close