1.建立連線DB LINK

USE [master]
GO

/****** Object:  LinkedServer [ADSI]    Script Date: 2018/03/06 上午 09:59:41 ******/
EXEC master.dbo.sp_addlinkedserver @server = N'ADSI', @srvproduct=N'Active Directory Services 2.5', @provider=N'ADSDSOObject', @datasrc=N'adsdatasource'
 /* For security reasons the linked server remote logins password is changed with ######## */
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'ADSI',@useself=N'False',@locallogin=NULL,@rmtuser=N'Account',@rmtpassword='Password'

GO

EXEC master.dbo.sp_serveroption @server=N'ADSI', @optname=N'collation compatible', @optvalue=N'false'
GO

EXEC master.dbo.sp_serveroption @server=N'ADSI', @optname=N'data access', @optvalue=N'true'
GO

EXEC master.dbo.sp_serveroption @server=N'ADSI', @optname=N'dist', @optvalue=N'false'
GO

EXEC master.dbo.sp_serveroption @server=N'ADSI', @optname=N'pub', @optvalue=N'false'
GO

EXEC master.dbo.sp_serveroption @server=N'ADSI', @optname=N'rpc', @optvalue=N'false'
GO

EXEC master.dbo.sp_serveroption @server=N'ADSI', @optname=N'rpc out', @optvalue=N'false'
GO

EXEC master.dbo.sp_serveroption @server=N'ADSI', @optname=N'sub', @optvalue=N'false'
GO

EXEC master.dbo.sp_serveroption @server=N'ADSI', @optname=N'connect timeout', @optvalue=N'0'
GO

EXEC master.dbo.sp_serveroption @server=N'ADSI', @optname=N'collation name', @optvalue=null
GO

EXEC master.dbo.sp_serveroption @server=N'ADSI', @optname=N'lazy schema validation', @optvalue=N'false'
GO

EXEC master.dbo.sp_serveroption @server=N'ADSI', @optname=N'query timeout', @optvalue=N'0'
GO

EXEC master.dbo.sp_serveroption @server=N'ADSI', @optname=N'use remote collation', @optvalue=N'true'
GO

EXEC master.dbo.sp_serveroption @server=N'ADSI', @optname=N'remote proc transaction promotion', @optvalue=N'true'
GO


 

2.利用OpenQuery 進行查詢

SELECT * FROM OpenQuery ( 
  ADSI,  
  'SELECT displayName, telephoneNumber, mail, mobile, facsimileTelephoneNumber ,userPrincipalName, sAMAccountName
  FROM  ''LDAP......'' 
  WHERE objectClass =  ''User'' and  sAMAccountName=''AD_ACCOUNT''
  ') AS tblADSI
ORDER BY displayname

3.可查詢的欄位可參考

  https://technet.microsoft.com/zh-tw/library/gg232586.aspx

 User 部分 只有 Domain 這個欄位似乎有問題外皆可查詢

User/Microsoft.AD.User

下表說明 Active Directory User 物件之屬性與 Service Manager Microsoft.AD.User 類別內容的對應。

 

Active Directory User 屬性 Microsoft.AD.User 內容

physicaldeliveryofficename

Office

displayname

displayname

company

Company

employeeid

Employeeid

department

Department

telephonenumber

BusinessPhone

homePhone

HomePhone

facsimileTelephoneNumber

Fax

mobile

Mobile

pager

Pager

mail

Email

givenname

FirstName

initials

Initials

sn

LastName

distinguishedname

Distinguishedname

title

Title

manager

manager

samaccountname

UserName

l

City

StreetAddress

StreetAddress

st

State

postalCode

Zip

co

Country

localeID

Locale

msRTCSIP-PrimaryUserAddress

SipAddress

objectSid

SID

Domain

Domain

 

Group/Microsoft.AD.UserBase

下表說明 Active Directory Group 物件之屬性與 Service Manager Microsoft.AD.UserBase 類別內容的對應。

 

Active Directory Group 屬性 Microsoft.AD.UserBase 內容

displayname

displayname

mail

Email

distinguishedname

Distinguishedname

samaccountname

samaccountname

objectSid

SID

Domain

Domain

Printer/Microsoft.AD.Printer

下表說明 Active Directory PrintQueue 物件之屬性與 Service Manager Microsoft.AD.Printer 類別內容的對應。

 

Active Directory Printer 屬性 Microsoft.AD.Printer 內容

uNCName

uNCName

serverName

serverName

shortServerName

shortServerName

printerName

printerName

printNetworkAddress

printNetworkAddress

printShareName

printShareName

isDeleted

isDeleted

driverName

driverName

driverVersion

driverVersion

printMemory

printMemory

printCollate

printCollate

printOwner

printOwner

assetNumber

assetNumber

managedBy

managedBy

printDuplexSupported

printDuplexSupported

printColor

printColor

printStaplingSupported

printStaplingSupported

versionNumber

versionNumber

url

url

printMediaSupported

printMediaSupported

printRateUnit

printRateUnit

printMaxXExtent

printMaxXExtent

printKeepPrintedJobs

printKeepPrintedJobs

printRate

printRate

printMediaReady

printMediaReady

printPagesPerMinute

printPagesPerMinute

printMaxResolutionSupported

printMaxResolutionSupported

printMACAddress

printBinNames

printMACAddress

printMACAddress

portName

portName

physicalLocationObject

physicalLocationObject

keywords

keywords

printNotify

printNotify

wWWHomePage

wWWHomePage

whenChanged

whenChanged

modifyTimeStamp

modifyTimeStamp

location

location

canonicalName

canonicalName

displayname

displayname

cn

Fullname

distinguishedname

Distinguishedname

description

description

Computer/Microsoft.Windows.Computer

下表說明 Active Directory Computer 物件之屬性與 Service Manager Microsoft.Windows.Computer 類別內容的對應。

 

Active Directory Computer 屬性 Microsoft.Windows.Computer 內容

msDS-SiteName

ActiveDirectorySite

dNSHostName

DNSName

ipHostNumber

IPAddress

networkAddress

NetworkName

msDS-PrincipalName

PrincipalName

displayname

displayname

samaccountname

NetbiosComputerName

objectSid

ActiveDirectoryObjectSid

ou

OrganizationalUnit

Domain

NetbiosDomainName