Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CREATE MAIL CONTACT FROM EXCHANGE POWERSHELL THROUGH SCRIPT

Posted on 2014-12-28
12
Medium Priority
?
245 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 71

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 71

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 71

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 71

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 71

Accepted Solution

by:
Qlemo earned 2000 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

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Sometimes clients can lose connectivity with the Lotus Notes Domino Server, but there's not always an obvious answer as to why it happens.   Read this article to follow one of the first experiences I had with Lotus Notes on a client's machine, my…
Are you an Exchange administrator employed with an organization? And, have you encountered a corrupt Exchange database due to which you are not able to open its EDB file. This article will explain all the steps to repair corrupt Exchange database.
In this video we show how to create an Address List 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 Organization >> Ad…
To show how to generate a certificate request 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 >> Certificates…

705 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