Strange Powershell Issue
I'm running Exchange 2007 SP1 with rollup 3. I'm getting a strange error when I try and write a script for a new email address list policy. Here is the code:
new-EmailAddressPolicy -NamePolicy1 -IncludedRecipients 'AllRecipients' -ConditionalCustomAttribute1Policy1 -EnabledEmailAddressTemplates 'SMTP:%s@domain1.com','smtp:%s@domain2.com'
This script works just fine if I paste it into an Exchange shell window, but when I run it as a powershell script, it gives me an error that says:
The -email address policy object "Policy1" is invalid because it contains invalid e-mail address templates
It works fine with one SMTP address, but not two (or more). I'm completely stumped, any suggestions?
September 22nd, 2008 11:35pm
Dan,i just tried to simulate your scenario and I have no problem with creating a script with comma separated templates. Try wrapping parenthesis around the collection and see if that works.
Free Windows Admin Tool Kit Click here and download it now
September 23rd, 2008 7:11am
try it without quote...
New-EmailAddressPolicy -NamePolicy1 -IncludedRecipients 'AllRecipients' -ConditionalCustomAttribute1Policy1 -EnabledEmailAddressTemplates SMTP:%s@domain1.com,smtp:%s@domain2.com
This has worked for me in script...
September 23rd, 2008 7:59am
I just realized that I wasn't clear enough before in my question. Yes, it works without the quotes, when typed directly into powershell, but not from a script. For example, my script uses variables from user inputs and the line in the script looks like this:
$PolicyName = read-host "Enter Policy Name"
$PolicyName = read-host "EnterFilter"
$PrimaryEmail = read-host "Enter First Domain"
$SecondaryEmail = read-host "EnterSecondDomain"
$EmailTemplate = "SMTP:%s@" + $PrimaryEmail + ",smtp:%s@" + $SecondaryEmail
New-EmailAddressPolicy -Name$PolicyName -IncludedRecipients 'AllRecipients' -ConditionalCustomAttribute1$Filter -EnabledEmailAddressTemplates $EmailTemplate
All of my variables are strings, but the $EmailTemplate causes an error. I'm stuck. I have even tried doing a write-host followed by the line above so that it outputs tothe screenexactly what it's trying to do, and if I copy and paste that into powershell, it works. When I run the script above, here is the output I get.
New-EmailAddressPolicy : The e-mail address policy object "Test" is invalid because it contains invalid e-mail address templates: 'SMTP:%s@domain1.com,smtp:%s@domain2.com'. To view details of the error, see the application event log in the Windows Event Viewer on the Microsoft Exchange Server computer.
The event logs on my mailbox server shows this:
Unable to generate an e-mail address because the site address SMTP:%s@domain1.com,smtp:%s@domain2.com is bad.
If I change the script above to only include one domain, it works. I'm stumped.
Free Windows Admin Tool Kit Click here and download it now
September 23rd, 2008 6:22pm
from the above script what you get for $EmailTemplate is a string "SMTP:%s@domain1.com,smtp:%s@domain2.com" instead of a collection. -EnabledEmailAddressTemplates accepts a string or collection of strings in the form of a template.So, try this:$EmailTemplate = "SMTP:%s@$PrimaryEmail","smtp:%s@$SecondaryEmail"New-EmailAddressPolicy -Name$PolicyName -IncludedRecipients 'AllRecipients' -ConditionalCustomAttribute1$Filter -EnabledEmailAddressTemplates $EmailTemplate
September 23rd, 2008 7:02pm
Thanks!I've almost got it. Your suggestion above worked, so now when I put this into practical use, there is one last issue that I'm running into.
I'm allowing the user to input multiple email addresses, seaprated by commas. I then parse them out in a while statement, and append onto the $EmailTemplate variable. However, with mutiple addresses, it isn't working, and I can only assume it goes back to the original problem.
Here's an example (keep in mind, I'm no scripting expert, so this probably isn't the most efficient way to do this).
$PrimaryEmail = "domain.com"
$AdditionalEmailDomains = "domain1.com,domain2.com,domain3.com"
$Loop = 1$FirstLoop = 1
$ParseAdditionalDomainNames = $AdditionalEmailDomains
while ($Loop -eq 1){ $IndexOf = $ParseAdditionalDomainNames.indexof(",")
if ($IndexOf -eq -1) {$ParsedDomain = $ParseAdditionalDomainNames$Loop = 0 } else { $ParsedDomain = $ParseAdditionalDomainNames.substring(0,$IndexOf)$ParseAdditionalDomainNames = $ParseAdditionalDomainNames.substring($IndexOf +1,$ParseAdditionalDomainNames.length - $IndexOf - 1)}$ProxyAddress = "smtp:" + $EmailAddressFormat + "@" + $ParsedDomain
if ($FirstLoop -eq 1) {$EmailAddressTemplates = "$ProxyAddress"$FirstLoop = 0 } else {$EmailAddressTemplates = "$EmailAddressTemplates","$ProxyAddress" }}
$ProxyAddress = "SMTP:%g@" + $PrimaryEmail
$EmailAddressTemplates = "$ProxyAddress","$EmailAddressTemplates"
It seems to want each address laid out individually, and putting them together in the $EmailAddressTemplates variable and then adding that at the end breaks it.
Free Windows Admin Tool Kit Click here and download it now
September 23rd, 2008 9:31pm
Dan,your problem with the code is in the line:$EmailAddressTemplates = "$EmailAddressTemplates","$ProxyAddress"Initiliaze the $EmailAddressTemplates as an empty array before the loop$EmailAddressTemplates = @()Then substitute it like this:$EmailAddressTemplates += "$ProxyAddress"Alternatively, Use the same contruct to gather the secondary domains via read-host commandlet$inputCollection = @()$inputStr = read-host -prompt "Obtain secondary domain"$inputCollection += "smtp:%s@$inputStr"$inputCollection += "SMTP:%s@$PrimaryEmail"
September 24th, 2008 12:13am
Awesome, that worked!
It's amazing how easy things are when someone who knows what they're doing shows you how to do it.
Free Windows Admin Tool Kit Click here and download it now
September 24th, 2008 12:20am