Solved

connect to an exchange 2013 address book

Posted on 2014-10-30
5
156 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
[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
  • 3
  • 2
5 Comments
 
LVL 11

Assisted Solution

by:Joe Klimis
Joe Klimis 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 11

Accepted Solution

by:
Joe Klimis 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
how to add IIS SMTP to handle application/Scanner relays into office 365.

724 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