Solved

connect to an exchange 2013 address book

Posted on 2014-10-30
5
143 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Great help  Thank you very much!!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
Exchange server is not supported in any cloud-hosted platform (other than Azure with Azure Premium Storage).
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

743 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

15 Experts available now in Live!

Get 1:1 Help Now