Solved

connect to an exchange 2013 address book

Posted on 2014-10-30
5
145 Views
Last Modified: 2014-11-13
Hey guys
I am pretty sure there is a way to do this but just in case I will ask here.  I need to be able to connect to an Exchange 2013 address book with a MS SQL command (search, lookup and insert)  I have a customer who wants to be able to add email addresses automatically from a notification that he receives from his Smart Host provider. Here is how it works.  His Smart Host provider will send notifications every time an email is sent from his domain.   The notifications will contain the sender and recipients name. He want to take the recipients name and see if it's a new address or one he has in his Exchange Address book. If he already has it the application will not add it to the database.  If it's a new one then the application we are creating using SQL server will insert the address.
I think i will probably be told to use Power Shell but just in case you guys have another suggestion I would sure appreciate it
0
Comment
Question by:jonmenefee
  • 3
  • 2
5 Comments
 
LVL 10

Assisted Solution

by:JoeKlimis
JoeKlimis earned 500 total points
ID: 40434882
Hi

I can think of a neat way of doing this , but you could run Powershell commands from sql  

Here is a good example

so you could try  something like this

xp_cmdshell 'PowerShell.exe -noprofile -command "
Add-pssnapin Microsoft.Exchange.Management.Powershell.Admin;  New-MailContact -Name ""Chris Ashton"" -ExternalEmailAddress ""Chris@tailspintoys.com"" -OrganizationalUnit ""Marketing"" "'

Open in new window

0
 

Author Comment

by:jonmenefee
ID: 40437308
That is great code. If the name is already in there would it return an error message?
0
 
LVL 10

Accepted Solution

by:
JoeKlimis earned 500 total points
ID: 40437672
It will do exactly the same as on the command line , it would issue you a warning , this can be captured like this


declare @cmdline varchar(500),@ReturnCode int
select @cmdline = 'PowerShell.exe -noprofile -command "Add-pssnapin Microsoft.Exchange.Management.Powershell.Admin;  New-MailContact -Name ""Chris Ashton"" -ExternalEmailAddress ""Chris@tailspintoys.com"" -OrganizationalUnit ""Marketing"" "'

create table #temp (PsErrors varchar(500))
insert #temp
EXEC @ReturnCode = master.dbo.xp_cmdshell @cmdline

if @ReturnCode <> 0
select * from #temp
where PsErrors is not null

Open in new window

0
 

Author Comment

by:jonmenefee
ID: 40437688
That is perfect!   So I just need to modify it to my server and the last one you sent is what I would use in SQL. :-)   thanks!!
0
 

Author Closing Comment

by:jonmenefee
ID: 40441791
Great help  Thank you very much!!
0

Featured Post

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

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.
Read this checklist to learn more about the 15 things you should never include in an email signature.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

863 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