Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert email address template string to valid email address

Status
Not open for further replies.
Aug 7, 2006
578
US
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.
 
Depends on how you want to input data. Do you want to do something like
Code:
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.
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top