Friday, December 31, 2010

Windows PowerShell Active Directory Sync to Sharepoint User Profiles

Download: Set-WSS-UserInfo-From-ActiveDirectory

Synchronizes all Site Collections User information between SharePoint (WSS or MOSS) and Active Directory
  • To execute this script, you have to :
    • Open the PS1 file
    • Modify the line 198: [ Set-UserInfoDetails "http://mySharePointWebApplication" "LDAP://DC=MyControler,DC=MyDomain,DC=com" ] with your internal parameters
    • Execute the script by use the file name "Set-WSS-UserInfo-From-ActiveDirectory.ps1"

All the User data will receives the information, you can modify the script if you have extended data.

Main Page: http://sharepointpsscripts.codeplex.com/releases/view/21699 



Warning:  
If any information is left blank in AD and you already filled in the information manually in SharePoint, such as Email address's or Job Title's. They will be over written with blank information. Please make sure that all the user information is updated in AD before running this script. 


I am in no way shape or form liable if anything should go wrong. 
Use this information at your own risk.


You've been warned.



Basic Setup

Download and install Windows PowerShell 2.0
Windows Management Framework Core (WinRM 2.0 and Windows PowerShell 2.0)

To run unsigned PowerShell scripts execute: 
Powershell -STA -command "& {Set-ExecutionPolicy -Scope LocalMachine Unrestricted -Force}
(The script supplied is unsigned, it will not run until you execute this command.)

Edit script at line 198 and save.
Set-UserInfoDetails "http://mySharePointWebApplication" "LDAP://DC=MyDomain,DC=com"
(If you have more than one domain, create a new script for each one.)

Customize what is written to Sharepoint: 

Starting at line 139.

Edit
if($Verbose){Write-Host "AD Result: ", $UserNtName, $UserPreferedName, $UserJob, $UserDpt, $UserSIP, $AboutMe, $UserEmail}

Edit        (Only Applies to Email Address)
# Set the basic UserInfo Data
                $MyUser.Name = [string]$myUserAD.Properties.name
                $MyUser.Email = $UserEmail

Edit      (This may not need to be edited, I did to make it look cleaner.)
# Set the detailed UserInfo Data
      $userInfoDetails["Name"] = $UserNtName
      $userInfoDetails["Title"] = $UserPreferedName
      $userInfoDetails["JobTitle"] = $UserJob
      $userInfoDetails["SipAddress"] = $UserSIP
      $userInfoDetails["Picture"] = "" #You can load it if you have the data in your AD
      $userInfoDetails["Notes"] = $AboutMe
      $userInfoDetails["EMail"] = $UserEmail
      $userInfoDetails["Department"] = $UserDpt




Example:  To sync just there name.

Starting at line 139.

if($Verbose){Write-Host "AD Result: ", $UserNtName, $UserPreferedName}

# Set the basic UserInfo Data
                $MyUser.Name = [string]$myUserAD.Properties.name


# Set the detailed UserInfo Data
     $userInfoDetails["Name"] = $UserNtName
     $userInfoDetails["Title"] = $UserPreferedName


Windows Schedule Tasks Setup
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe &'C:\SharePoint AD Sync Scripts\Set-WSS-UserInfo-From-ActiveDirectory-.ps1'

Set the task run as to your SharePoint service account. (Example: Domain\SharePoint)

(I set the task to run once a day at 12PM.)

If you found this post helpful, please leave comments below.

1 comment: