for SharePoint Online
Update a single user profile property with a simple powershell script.
Please use the link below to get the powershell script downloaded.
This script example is updating/refreshing all UserProfile's SPS-Birthday value to force the search crwaler reindexing the profiles.
Please note, the used service account need to login without MFA, so the permission can be set to update all profiles with administrator previliges.
The script is working the following way:
# Dev
$environment = "Dev"
$webURL = "https://contoso.sharepoint.com"
$SpConnectAccount = "Service Account"
$WriteUPS = $false #$true will write the changes, $false will only generate the logfiles (testing)
#
##>
#########################
# Don't modify below here
#########################
$currentFolder = Convert-Path .
$OutPutView = $currentFolder
$scriptName = split-path $MyInvocation.MyCommand.Definition -Leaf
$scriptName = $scriptName.Replace(".ps1","")
$logFile = $currentFolder + "\" + $scriptName + "_" + $(get-date -f MMddyyyy_hhmmss) + ".log"
$reportErrors = [System.Collections.ArrayList]@()
Function ConnectWeb(){
param
(
[Parameter(Mandatory=$true)] [string] $webUrl
)
$web = $null;
try{
$web = Get-PnPWeb
Write-Host "Current Web is: " $web.Url
if ($web.Url -ne $webUrl){
Write-Host "Connecting to : " $webUrl
}
}catch{
Write-Host "Currently not connected. Trying to connect..."
}
if ($web -eq "NULL" -or $web.Url -ne $webUrl){
if ($environment -eq "Prod"){
Write-Host "PROD"
$clientid = "xxxx"
$tenant = "xxxx"
$CertThumbprint = "xxxx"
try{
Connect-PnPOnline -ClientId $clientId -Url $webUrl -Tenant $tenant -Thumbprint $CertThumbprint
}catch{
write-host "ClientID: " $clientid
write-host "tenant: " $tenant
write-host "Cert: " $CertThumbprint
write-host "webUrl: " $webUrl
write-host "EXCEPTION. Line: " $_.InvocationInfo.ScriptLineNumber -ForegroundColor Red
write-host "EXCEPTION: " $_.Exception.Message -ForegroundColor Red
}
sleep 3
}elseif ($environment -eq "Dev"){
Write-Host "DEV"
Connect-PnPOnline -S -Url $webUrl -Credentials $SpConnectAccount
sleep 3
}
}
}
Function startImport() {
LogMessage("Processing has started")
try {
ConnectWeb -webUrl $webURL
$web = Get-PnPWeb
# Verify web exists
if ($web -eq "NULL")
{
$outputText = "ERROR. Could not load web: " + $webURL;
LogError($outputText);
LogMessage("Processing has ended")
return
}
else
{
$outputText = "Connected to web " + $webURL
LogMessage($outputText)
}
# Put line break in output between each list entry
LogMessage("");
Write-host "Start updating UPS" -ForegroundColor Green
try {
$ProfileList = Submit-PnPSearchQuery -Query '-AccountName:spofrm -AccountName:spoapp -AccountName:app@sharepoint -AccountName:spocrawler -AccountName:spocrwl -PreferredName:"Foreign Principal"' -SourceId "b09a7990-05ea-4af9-81ef-edfab16c4e31" -SelectProperties "AccountName" -All -TrimDuplicates:$false -RelevantResults -ErrorAction Stop
}
catch {
Write-Error "Error with Submit-PnPSearchQuery"
Write-Error "Please check connection and permissions to $url and try again"
Write-Error $_
return
}
foreach ($Profile in $ProfileList)
{
$accountNames = [string]::Format($Profile.AccountName)
$userProperty = Get-PnPUserProfileProperty -Account $Profile.AccountName
$birthdayValue = $userProperty.UserProfileProperties.'SPS-Birthday'
LogMessage($accountNames + "," + $birthdayValue)
if ($birthdayValue.length -gt 7)
{
LogMessage("Update UPS")
#Check if date is correct formated
$fakeBirthday = ""
if ($birthdayValue.indexOf("/") -gt 0)
{
$dateArr = $birthdayValue.split(" ")[0]
$dateArr = $dateArr.split(".")
$birthdayValue = $dateArr[0]+"/"+$dateArr[1]+"/"+$dateArr[2]+" 00:00:00"
$fakeBirthday = $dateArr[0]+"/"+$dateArr[1]+"/2010 00:00:00"
}
elseif($birthdayValue.indexOf(".") -gt 0)
{
#DD.MM.YYYY HH:mm:ss
$dateArr = $birthdayValue.split(" ")[0]
$dateArr = $dateArr.split(".")
$birthdayValue = $dateArr[1]+"/"+$dateArr[0]+"/"+$dateArr[2]+" 00:00:00"
$fakeBirthday = $dateArr[1]+"/"+$dateArr[0]+"/2010 00:00:00"
}
if ($WriteUPS -eq $true)
{
LogMessage("Write fakebirthday " + $fakeBirthday)
Set-PnPUserProfileProperty -Account $Profile.AccountName -PropertyName "SPS-Birthday" -Value $fakeBirthday
}
else
{
LogMessage("[Info]fakebirthday " + $fakeBirthday)
}
if ($WriteUPS -eq $true)
{
LogMessage("Write orignial birthday " + $birthdayValue)
Set-PnPUserProfileProperty -Account $Profile.AccountName -PropertyName "SPS-Birthday" -Value $birthdayValue
}
else
{
LogMessage("[Info] Orignial birthday " + $birthdayValue)
}
LogMessage("")
}
}
}
catch [Exception]
{
write-host "EXCEPTION. Line: " $_.InvocationInfo.ScriptLineNumber -ForegroundColor Red
$outputText = "EXCEPTION: " + $_.Exception.Message
LogError($outputText);
}
finally
{
if ($webReport) { $webReport.Dispose()
}
}
LogMessage("Processing has ended")
}
Function LogError($outputText){
write-host $outputText -f Red
$logOutput = $(get-date -f g).ToString() + " - " + $outputText
write-output $logOutput | out-File $logFile -Append
($reportErrors.Add($logOutput)) | Out-Null
}
Function LogMessage($outputText){
write-host $outputText
$logOutput = $(get-date -f g).ToString() + " - " + $outputText
write-output $logOutput | out-File $logFile -Append
}
# Run the main function
startImport