Sharepoint 2010 Address Book from AD with Sync

I processed a large amount of material on this topic, however, I did not find anything more or less worthwhile. In this regard, he assembled his own version of the address book, requiring a minimum of time for implementation in the finished infrastructure.

Initial data:

  • Domain Level: Windows 2008 R2
  • Sharepoint Server: Windows Server 2012 Standart
  • Sharepoint 2010 version with all updates


Goal of the task

The goal of the task was to quickly deploy a dynamically updated telephone directory, whose data source would be Active Directory.

The solution of the problem

My solution to this task will not be something revolutionary for Sharepoint professionals, however, it may be interesting for novice system administrators who face similar tasks. For those interested, I ask for cat.

So, the whole task can be divided into two stages:
  • Export data from Active Directory.
  • Import data into a Sharepoint list.

Export data from AD


To export data from AD, use the Active Directory module for PowerShell 2 or 3 versions. The cmdlet that interests us is called Get-ADUser and has extensive help. We use it: The result of executing this code will be a table with the data of all users of the domain, including service accounts, however, in this table there will be no data such as phone numbers, email addresses, departments and employee positions. This is because, by default, PS returns only 10 AD-related properties. To indicate the need for PS to extract the parameters we need, we use the parameter of the Get-ADUsers -Parameters cmdlet (sorry for taftology).

Get-ADUser -Filter {(ObjectClass -eq "user")} | FT Surname, GivenName, Name, Mail, Company, Department, Title, telephoneNumber, homephone, mobile, facsimileTelephoneNumber




Get-ADUser -Filter {(ObjectClass -eq "user")} -Properties Surname, GivenName, Name, Mail, Company, Department, Title, telephoneNumber, homephone, mobile, facsimileTelephoneNumber | FT Surname, GivenName, Name, Mail, Company, Department, Title, telephoneNumber, homephone, mobile, facsimileTelephoneNumber

This code will return all the necessary data in a table form for all users of the domain. It is logical to assume that in the domain environment there is a large amount of service credentials. Add a filter that rejects user accounts with an empty “GivenName” field, that is, “Name”.

Get-ADUser -Filter {(ObjectClass -eq "user") -and (GivenName -like "*")} -Properties Surname, GivenName, Name, Mail, Company, Department, Title, telephoneNumber, homephone, mobile, facsimileTelephoneNumber | FT Surname, GivenName, Name, Mail, Company, Department, Title, telephoneNumber, homephone, mobile, facsimileTelephoneNumber

Now it is the turn to export the received data to a CSV file. (Initially, it was planned to do everything in one script, however, PowerShell 3 does not support Sharepoint 2010 cmdlets, and Powershell 2 cannot work with the Active Directory module designed for PowerShell 3). To export data, I used the standard PS Export-CSV cmdlet and stepped on a rake:
  1. The exported data were of the type 4641548913248745 ,, . Not at all what was output to the console earlier. Conclusion: tabular data does not suit us. Replace FT with SELECT and get ...
  2. ... a bunch of questions instead of Cyrillic characters. If the data has Cyrillic, you must explicitly set the export encoding (Unicode).

It is also worth sorting the data in alphabetical order by the “Name” or “Full Name” field so as not to force Sharepoint to do this, as well as remove the first line from the CSV file (the header left by PowerShell).

Get-ADUser -Filter {(ObjectClass -eq "user") -and (GivenName -like "*")} -Properties Surname, GivenName, Name, Mail, Company, Department, Title, telephoneNumber, homephone, mobile, facsimileTelephoneNumber | sort-object -property Name | Select Surname, GivenName, Name, Mail, Company, Department, Title, telephoneNumber, homephone, mobile, facsimileTelephoneNumber | Export-Csv D:\Contacts.csv -Encoding Unicode -NoTypeInformation


Import data to a Sharepoint List


Imagine that we have some Sharepoint portal available at portal , as well as a standard “Contacts” list into which we want to import data from AD. To connect to Sharepoint via PowerShell, you must add the Sharepoint remnant to PowerShell, which is included with Sharepoint. The connection to the “Contacts” list located on the “ portalportal is as follows:

add-pssnapin microsoft.sharepoint.powershell
$web = Get-SPWeb "http://portal"
$list = $web.lists["Контакты"]

I don’t see much sense in parsing this list, so at each synchronization I suggest deleting all its entries using the cycle:

do {$list.items.delete(0)}
until ($list.items.Count -eq 0)

Well, actually the import itself:

Import-Csv D:\Contacts.csv | ForEach-Object {
$item = $list.items.Add()
$item["Полное имя"] = $_.Name
$item["Организация"] = $_.Company
$item["Рабочий телефон"] = $_.telephoneNumber
$item["Мобильный телефон"] = $_.mobile
$item["Адрес электронной почты"] = $_.Mail
$item["Должность"] = $_.Title
$item.update()
}

Add other parameters to taste ...

PowerShell 2 from under PowerShell 3


Remember that I installed Sharepoint 2010 on Windows Server 2012? So here is another rake! There are several solutions: you can split the export from AD and the import into Sharepoint in time, or you can run PowerShell 2 from under PowerShell 3 (the messenger knows a lot ... (s)). To do this, we need only one line in the first script: Actually that's all. It remains only to drop C: \ Windows \ System32 \ WindowsPowerShell \ v1.0 \ PowerShell.exe with the parameter "& 'D: \ Set-Contacts.ps1'" into the task scheduler and keep the data of AD users up to date. Thank you for your time! I leave full versions of scripts below. Get-Contacts.ps1 Set-Contacts.ps1

C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -version 2.0 "&'D:\Set-Contacts.ps1'"





Get-ADUser -Filter {(ObjectClass -eq "user") -and (GivenName -like "*")} -Properties Surname, GivenName, Name, Mail, Company, Department, Title, telephoneNumber, homephone, mobile, facsimileTelephoneNumber | sort-object -property Name | Select Surname, GivenName, Name, Mail, Company, Department, Title, telephoneNumber, homephone, mobile, facsimileTelephoneNumber | Export-Csv D:\Contacts.csv -Encoding Unicode -NoTypeInformation
C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -version 2.0 " & ' D:\Set-Contacts.ps1 ' "


add-pssnapin microsoft.sharepoint.powershell
$web = Get-SPWeb "http://portal"
$list = $web.lists["Контакты"]
do {$list.items.delete(0)}
until ($list.items.Count -eq 0)
Import-Csv D:\Contacts.csv | ForEach-Object {
$item = $list.items.Add()
$item["Полное имя"] = $_.Name
$item["Организация"] = $_.Company
$item["Рабочий телефон"] = $_.telephoneNumber
$item["Мобильный телефон"] = $_.mobile
$item["Адрес электронной почты"] = $_.Mail
$item["Должность"] = $_.Title
$item.update()
}

Also popular now: