Solved

CREATE MAIL CONTACT FROM EXCHANGE POWERSHELL THROUGH SCRIPT

Posted on 2014-12-28
12
98 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
  • 6
  • 5
12 Comments
 
LVL 68

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
 

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 68

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 68

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 68

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 68

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Local Continuous Replication is a cost effective and quick way of backing up Exchange server data. The following article describes the steps required to configure Local Continuous Replication. Also, the article tells you how to restore from a backup…
Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
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…
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now