2017年10月15日 星期日
Get mail account from AD at MSSQL
1. Create the linked server
EXEC sp_addlinkedserver @server = 'ADSI',
@srvproduct = 'Active Directory Services 2.5',
@provider = 'ADSDSOObject',
@datasrc = 'adsdatasource'
2. Security
3. SQL Function Script
ALTER FUNCTION [dbo].[GetEmailFromAD]
(
-- Add the parameters for the function here
@pUserID varchar(250)
)
RETURNS varchar(250)
AS
BEGIN
-- Declare the return variable here
DECLARE @pMail varchar(250)
-- Add the T-SQL statements to compute the return value here
SELECT @pMail = mail
FROM OpenQuery (ADSI, '
SELECT displayName,
mail,
sAMAccountName
FROM ''LDAP://...:389/DC=...,DC=...,DC=...''
WHERE objectCategory = ''User''
')
WHERE sAMAccountName = RIGHT(@pUserID,LEN(@pUserID)-CHARINDEX('\',@pUserID))
-- Return the result of the function
RETURN @pMail
END
訂閱:
張貼留言 (Atom)


沒有留言:
張貼留言