⬆️ ⬇️

Address Book Sharepoint 2010 from AD with synchronization

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



Initial data:






Purpose of the task


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

')

The solution of the problem


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



So, the whole task can be divided into two stages:



Export data from AD



To export data from AD, use the Active Directory module for PowerShell 2 or 3 versions. The cmdlet we are interested in is called Get-ADUser and has a comprehensive help. We use it:



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





The result of executing this code will be a table with the data of all users of the domain, including service accounts, however, this table will not contain such data as phone numbers, email addresses, departments and positions of employees. This is due to the fact that by default PS returns only 10 account properties associated with accounts AD. To indicate the PS need 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")} -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 tabular form for all users of the domain. It is logical to assume that in the domain environment there is a large number of service credentials. Add a filter rejecting user accounts with a blank “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 comes the turn of exporting the data to a CSV file. (Initially, everything was done in one script, however, PowerShell 3 does not support Sharepoint 2010 cmdlets, and Powershell 2 cannot work with Active Directory module intended for PowerShell 3). To export data, I used the standard PS Export-CSV cmdlet and stepped on a rake:

  1. The exported data was of the form 4641548913248745 ,, . Not at all what was displayed in the console earlier. Conclusion: the table data does not suit us. Replace FT with SELECT and get ...
  2. ... a bunch of questions instead of Cyrillic characters. If the data is Cyrillic, you must explicitly set the export encoding (Unicode).


It is also worth ordering the data in alphabetical order by the "Name" or "Full Name" field, in order not to force Sharepoint to do this, and also 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 Sharepoint List



Imagine that we have some Sharepoint portal available at the portal , as well as a standard list of "Contacts" into which we want to import data from AD. To connect to Sharepoint via PowerShell, you need to add the Sharepoint folder to PowerShell, which is included in the Sharepoint package. The connection to the “Contacts” list we need, located on the portal “ portal ” has the form:



add-pssnapin microsoft.sharepoint.powershell

$web = Get-SPWeb "http://portal"

$list = $web.lists[""]



I can't see this list of parsings, so every synchronization I suggest I delete all of its entries using a cycle:



do {$list.items.delete(0)}

until ($list.items.Count -eq 0)



Well, actually 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 options to taste ...



PowerShell 2 from PowerShell 3



Remember that I put Sharepoint 2010 on Windows Server 2012? So here's another rake! There are several solutions: you can spread export from AD and import to Sharepoint by time, or you can run PowerShell 2 from under PowerShell 3 (Monsieur knows what it means ... (c)). For this we need just one line in the first script:



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





Actually that's all. It remains only to throw 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 the full versions of the scripts below.



Get-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 ' "



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()

}

Source: https://habr.com/ru/post/175921/



All Articles