Office 365 – SharePoint Online – PowerShell script : Synchronize the user profile properties from AD to SharePoint Online user properties
Hi All,
Recently, I got a chance to write PowerShell script for syncing user profile properties from Azure AD to SharePoint Online.
Here I’ll share PowerShell script which synchronizes Mobile Phone and City.
- Get the parameters:
- Credential File Path- with UserName and Password on two different lines
- Convert Password into secure string
- Admin site URL
- Import respective libraries
- Connect AzureAD
- Connect SharePoint Online
- Get the instance of PeopleManager
- Fetch all users from AzureAD
- Loop through all AzureAD users
- Read the properties which we want to synchronize
- Use People Manager SetSingleValueProfileProperty() to synchronize user profile properties in SharePoint Online
PowerShell Script: please ignore color applied to the following script. I still need to investigate for the code section in wordpress:)
<#
.SYNOPSIS
Sync given SPO user profile properties with Azure AD values
.PARAMETER CredentialFilePath
Office 365 system account credential file path having two lines in
following format
UserName
Password
.PARAMETER SPOAdminURL
SharePoint Online Admin Site URL
#>
param
(
[parameter(Mandatory=$true)][string]$CredentialFilePath,
[parameter(Mandatory=$true)][string]$SpoAdminUrl,
[parameter(Mandatory=$false)][string]$LogFolderPath = "c:\"
)
if(-not(Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"}))
{
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
Import-Module MSOnline
Import-Module Microsoft.Online.SharePoint.PowerShell
# add SharePoint CSOM libraries on given path
Import-Module 'C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'
Import-Module 'C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
Import-Module 'C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll'
#Function to write the log. Put all logs in log.txt
Function LogWrite
{
Param ([string]$logstring)
$Logfile = $LogFolderPath + "\log.txt"
Add-content $Logfile -value $logstring
}
Try {
LogWrite "Syncing the AD Properties"
#Get the user credential file path and getting user from it
$user = Get-Content $CredentialFilePath | Select-Object -First 1
#Getting password
$password = Get-Content $CredentialFilePath | Select-Object -First 1 -Skip 1
$securePassword = ConvertTo-SecureString $Password -AsPlainText -Force
#Credential object
$credential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $user, $securePassword
# Connect to AzureAD
Connect-MsolService -Credential $credential
LogWrite "Azure Connected"
# Get credentials for SharePointOnline
$spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.GetNetworkCredential().Username, (ConvertTo-SecureString $credential.GetNetworkCredential().Password -AsPlainText -Force))
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SpoAdminUrl)
$ctx.Credentials = $spoCredentials
$spoPeopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx)
# Get all AzureAD Users
$AzureADUsers = Get-MSolUser -All
#Here, we are also writting the CSV file. Adding headings to CSV file.
Add-Content -Path C:\Users.csv -Value '"MobilePhone","City","Street Address","Country","TargetSPOUserAccount"'
#looping through all the AD users and getting respective properties which we need to sync
ForEach ($AzureADUser in $AzureADUsers) {
#mobile phone
$mobilePhone = $AzureADUser.MobilePhone
#city
$city = $AzureADUser.City
#getting the user name
$targetUPN = $AzureADUser.UserPrincipalName.ToString()
#SPO formatting user
$targetSPOUserAccount = ("i:0#.f|membership|" + $targetUPN)
LogWrite "Synchronising the user - $targetUPN"
#preparing string to write all users in CSV file
$line = $mobilePhone +"," + $city +"," + $streetAddress +"," + $country + "," + $targetSPOUserAccount;
#writting to CSV file
Add-Content -Path C:\Users.csv -Value $line
$cellPhone_PropertyName = "CellPhone"
$office_PropertyName = "Office"
$userCellPhone = $targetUserCellPhone.Value
#SetSingleValueProfileProperty - updating SPO user profile for mobile phone and city
$spoPeopleManager.SetSingleValueProfileProperty($targetspoUserAccount, $cellPhone_PropertyName, $mobilePhone)
$spoPeopleManager.SetSingleValueProfileProperty($targetspoUserAccount, $office_PropertyName, $city)
$ctx.ExecuteQuery()
} #foreach
LogWrite "All users properties are synchronised successfully"
}
Catch {
[Exception]
LogWrite $Error
}
References:
Thanks for reading 🙂
Keep reading, share your thoughts, experiences. Feel free to contact us to discuss more. If you have any suggestion / feedback / doubt, you are most welcome.
Stay tuned on Knowledge-Junction, will come up with more such articles