Solved

connect to an exchange 2013 address book

Posted on 2014-10-30
5
148 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
A procedure for exporting installed hotfix details of remote computers using powershell
Viewers will learn how the fundamental information of how to create a table.
how to add IIS SMTP to handle application/Scanner relays into office 365.

773 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