Recently, I got a chance to write a PowerShell script for syncing the user profile properties from Azure AD to SharePoint Online.

Here, I’ll share a PowerShell script which synchronizes the mobile phone and city.

High-level steps
  1. Get the parameters,

    • Credential File Path- with Username and Password on two different lines.
    • Convert the password into a secure string
    • Admin the site URL.

  2. Import respective libraries.
  3. Connect AzureAD.
  4. Connect SharePoint Online.
  5. Get the instance of PeopleManager.
  6. Fetch all users from AzureAD.
  7. Loop through all AzureAD users.

    • Read the properties which we want to synchronize.
    • Use PeopleManager SetSingleValueProfileProperty() to synchronize the user profile properties in SharePoint Online.
PowerShell Script
  1. <#
  2. .SYNOPSIS
  3. Sync given SPO user profile properties with Azure AD values
  4. .PARAMETER CredentialFilePath
  5. Office 365 system account credential file path having two lines in following format
  6. UserName
  7. Password
  8. .PARAMETER SPOAdminURL
  9. SharePoint Online Admin Site URL
  10. #>
  11. param
  12. (
  13. [parameter(Mandatory=$true)][string]$CredentialFilePath,
  14. [parameter(Mandatory=$true)][string]$SpoAdminUrl,
  15. [parameter(Mandatory=$false)][string]$LogFolderPath = "c:\"
  16. )
  17. if(-not(Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"}))
  18. {
  19. Add-PSSnapin Microsoft.SharePoint.PowerShell;
  20. }
  21. Import-Module MSOnline
  22. Import-Module Microsoft.Online.SharePoint.PowerShell
  23. # add SharePoint CSOM libraries on given path
  24. Import-Module 'C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'
  25. Import-Module 'C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
  26. Import-Module 'C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll'
  27. #Function to write the log. Put all logs in log.txt
  28. Function LogWrite
  29. {
  30. Param ([string]$logstring)
  31. $Logfile = $LogFolderPath + "\log.txt"
  32. Add-content $Logfile -value $logstring
  33. }
  34. Try {
  35. LogWrite "Syncing the AD Properties"
  36. #Get the user credential file path and getting user from it
  37. $user = Get-Content $CredentialFilePath | Select-Object -First 1
  38. #Getting password
  39. $password = Get-Content $CredentialFilePath | Select-Object -First 1 -Skip 1
  40. $securePassword = ConvertTo-SecureString $Password -AsPlainText -Force
  41. #Credential object
  42. $credential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $user, $securePassword
  43. # Connect to AzureAD
  44. Connect-MsolService -Credential $credential
  45. LogWrite "Azure Connected"
  46. # Get credentials for SharePointOnline
  47. $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.GetNetworkCredential().Username, (ConvertTo-SecureString $credential.GetNetworkCredential().Password -AsPlainText -Force))
  48. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SpoAdminUrl)
  49. $ctx.Credentials = $spoCredentials
  50. $spoPeopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx)
  51. # Get all AzureAD Users
  52. $AzureADUsers = Get-MSolUser -All
  53. #Here, we are also writting the CSV file. Adding headings to CSV file.
  54. Add-Content -Path C:\Users.csv -Value '"MobilePhone","City","Street Address","Country","TargetSPOUserAccount"'
  55. #looping through all the AD users and getting respective properties which we need to sync
  56. ForEach ($AzureADUser in $AzureADUsers) {
  57. #mobile phone
  58. $mobilePhone = $AzureADUser.MobilePhone
  59. #city
  60. $city = $AzureADUser.City
  61. #getting the user name
  62. $targetUPN = $AzureADUser.UserPrincipalName.ToString()
  63. #SPO formatting user
  64. $targetSPOUserAccount = ("i:0#.f|membership|" + $targetUPN)
  65. LogWrite "Synchronising the user - $targetUPN"
  66. #preparing string to write all users in CSV file
  67. $line = $mobilePhone +"," + $city +"," + $streetAddress +"," + $country + "," + $targetSPOUserAccount;
  68. #writting to CSV file
  69. Add-Content -Path C:\Users.csv -Value $line
  70. $cellPhone_PropertyName = "CellPhone"
  71. $office_PropertyName = "Office"
  72. $userCellPhone = $targetUserCellPhone.Value
  73. #SetSingleValueProfileProperty - updating SPO user profile for mobile phone and city
  74. $spoPeopleManager.SetSingleValueProfileProperty($targetspoUserAccount, $cellPhone_PropertyName, $mobilePhone)
  75. $spoPeopleManager.SetSingleValueProfileProperty($targetspoUserAccount, $office_PropertyName, $city)
  76. $ctx.ExecuteQuery()
  77. } #foreach
  78. LogWrite "All users properties are synchronised successfully"
  79. }
  80. Catch {
  81. [Exception]
  82. LogWrite $Error
  83. }

References