Solved

CREATE MAIL CONTACT FROM EXCHANGE POWERSHELL THROUGH SCRIPT

Posted on 2014-12-28
12
143 Views
Last Modified: 2015-03-19
Dear Experts,

I want to create multiple  mail contacts through powershell.
Can i have script and example of CSV file.
0
Comment
Question by:msretailit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 70

Expert Comment

by:Qlemo
ID: 40520311
That is easy - to some extent.  You need to tell what properties you want to set for a contact, that determines how the CSV file should look like. Basically, the code is:
Import-CSV c:\temp\mailcontacts.csv | % {
  New-MailContact -name $_.Name -ExternalEmailAddress $_.EMail
}

Open in new window

The CSV file:
Name,EMail,OtherStuff
Me,me@mydomain.com,Not used

Open in new window

OtherStuff is not used here, just showing that you can have additional columns.
0
 

Author Comment

by:msretailit
ID: 40520373
Got this error

Import-Csv : Cannot process argument because the value of argument "name" is invalid. Change the value of the "name" ar
gument and run the operation again.
At line:1 char:11
+ Import-CSV <<<<  c:\temp\mailcontacts.csv | % {
    + CategoryInfo          : InvalidArgument: (:) [Import-Csv], PSArgumentException
    + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.ImportCsvCommand
0
 
LVL 19

Expert Comment

by:R--R
ID: 40520386
You have to use the command in a single line.

You can also check this if it works.

Create an input file name contacts.csv with the following attributes and values
 
name,firstname,lastname,externalemailaddress

Open the Exchange management shell and run the below cmd after copying all contacts to the path of shell:

Import-Csv “contacts.csv” | ForEach {New-MailContact -Name $_.Name -Firstname $_.FirstName -LastName $_.LastName -ExternalEmailAddress $_.ExternalEmailAddress -OrganizationalUnit “Domainname/OUname”}

Reffered from http://msexchangeguru.com/2012/11/16/contacts-in-bulk/
(Prabhat Nigam)
0
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 

Author Comment

by:msretailit
ID: 40520399
OK i got below command working

" Import-Csv C:\ExternalContacts.csv|%{New-MailContact -Name $_.Name -DisplayName $_.Name -ExternalEmailAddress $_.ExternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName}"

and csv file saved was externalemailaddress, name, firstname,lastname.

I got add all the contacts by above command.

But i want to remove tickmark of automatic update email address policy from contacts and also i want to add email address.
Anyone have idea.
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40520417
Note that the shown command is not different from what I posted. I just left out some fields, as I don't know what you need. The error shown (usually) results from the Name not being in the expected format to automatically determine FirstName and LastName from it.
Whatsoever, the CSV is settled now. But I don't get what you mean by "add email address" - this is exactly what you do. Or do you need to add more than one address?
The other requirement can be achieved if we set PrimarySmtpAddress, because that will disable the email policy setting too:
Import-Csv C:\ExternalContacts.csv| % {
  New-MailContact -Name $_.Name -DisplayName $_.Name -PrimarySmtpAddress $_.ExternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName
}

Open in new window

0
 

Author Comment

by:msretailit
ID: 40520433
Hello Olemo,

I have already made contacts with above command but now it make external address as primary addreses.

Now i want to disable email policy setting from all that contacts and want to add one more addresse
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40520489
As said, setting PrimarySmtpAddress disables the email policy setting, so no further action required.

What do you gain from adding email addresses? Remember: an Exchange Contact is not the same as a Outlook Contact. I'm not certain that you can select a secondary email address when sending email; however, it will map incoming mails against the contact info for all email addresses.

Whatsover, if you insist, add a column "OtherAddresses" to your CSV file, having comma separated addresses enclosed in double quotes.
externalemailaddress, name, firstname,lastname, otherAddresses
me@mydomain.com, It is Me, It, Me, "smtp:me@otherdomain.com, smtp:me@goggle.com"

Open in new window

Then use
Import-Csv C:\ExternalContacts.csv| % {
  New-MailContact -Name $_.Name -DisplayName $_.Name -PrimarySmtpAddress $_.ExternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName |
    Set-MailContact -EmailAddresses ("smtp:"+$_.ExternalEmailAddress; $_.OtherAddresses -split ',') -EmailAddressPolicyEnabled $false
}

Open in new window

Please see http://technet.microsoft.com/en-us/library/aa995950(v=exchg.141).aspx and http://technet.microsoft.com/en-us/library/bb124519(v=exchg.141).aspx for details about what you can set with which cmdlet.
0
 

Author Comment

by:msretailit
ID: 40667760
Dear Team,

I got below error


[PS] C:\Windows\system32>Import-Csv C:\TEMP\EmailCreation.csv| % {New-MailContact -Name $_.Name -DisplayName $_.Name -Pr
imarySmtpAddress $_.ExternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName | Set-MailContact -EmailAddresses
 ("smtp:"+$_.ExternalEmailAddress; $_.OtherAddresses -split ',') -EmailAddressPolicyEnabled $false}
Missing closing ')' in expression.
At line:1 char:249
+ Import-Csv C:\TEMP\EmailCreation.csv| % {New-MailContact -Name $_.Name -DisplayName $_.Name -PrimarySmtpAddress $_.Ex
ternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName | Set-MailContact -EmailAddresses ("smtp:"+$_.External
EmailAddress <<<< ; $_.OtherAddresses -split ',') -EmailAddressPolicyEnabled $false}
    + CategoryInfo          : ParserError: (CloseParenToken:TokenId) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndParenthesisInExpression
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40668267
I'm not amused having to deal with this after 3 months without any response ...
Import-Csv C:\ExternalContacts.csv| % {
  New-MailContact -Name $_.Name -DisplayName $_.Name -PrimarySmtpAddress $_.ExternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName |
    Set-MailContact -EmailAddresses $("smtp:"+$_.ExternalEmailAddress; $_.OtherAddresses -split ',') -EmailAddressPolicyEnabled $false
}

Open in new window

0
 

Author Comment

by:msretailit
ID: 40669804
Hey Olemo,

Sorry for delay due to some issues.

I got below error again


[PS] C:\Windows\system32>Import-Csv C:\TEMP\EmailCreation.csv| % { New-MailContact -Name $_.Name -DisplayName $_.Name -P
rimarySmtpAddress $_.ExternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName | Set-MailContact -EmailAddresse
s $("smtp:"+$_.ExternalEmailAddress; $_.OtherAddresses -split ',') -EmailAddressPolicyEnabled $false }

cmdlet New-MailContact at command pipeline position 1
Supply values for the following parameters:
ExternalEmailAddress:
Cannot process argument transformation on parameter 'ExternalEmailAddress'. Cannot convert value "" to type "Microsoft.
Exchange.Data.ProxyAddress". Error: "The e-mail address cannot be empty."
    + CategoryInfo          : InvalidData: (:) [New-MailContact], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,New-MailContact
0
 

Author Comment

by:msretailit
ID: 40669805
This is csv file


ExternalEmailAddress, name, firstname,lastname, otherAddresses
1@gmail.com,M D,M,D,1@Officedomain.com
0
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40673374
Obviously we have to set both the ExternalEmailAddress and PrimarySmtpAddress here.
Import-Csv C:\ExternalContacts.csv| % {
  New-MailContact -Name $_.Name -DisplayName $_.Name -ExternalEmailAddress $_.ExternalEmailAddress `
        -PrimarySmtpAddress $_.ExternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName |
    Set-MailContact -EmailAddresses $("smtp:"+$_.ExternalEmailAddress; $_.OtherAddresses -split ',') -EmailAddressPolicyEnabled $false
}

Open in new window

0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
In this video we show how to create a mailbox database in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Servers >> Data…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question