Hi I am running a powershell script to update the custom user profile property called 'testvalue' whose values are [2,3] format. We have total of 20,000 users but at 2300 th user the powershell script threw an error below
Error: : Exception calling "ExecuteQuery" with "0" argument(s): "The operation has timed out"
Below is the script
#Importing PowerShell Module
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
#Authenticating User to SharePoint Online
$username = "[email protected]"
$Adminurl = "https://siteoffice-admin.sharepoint.com"
$siteColUrl = "https://siteoffice.sharepoint.com"
#Logging Method for errors
$DATE = get-date
$LogPath = "C:\Work\UpdateUserProfile"
$LogFileName = "LogFileName.log"
$FilePath = $LogPath +"\" + $LogFileName
$logFileCreated = $False
function write-log([string]$label, [string]$logMsg)
{
if($logFileCreated -eq $False)
{
write-host "Creating log file..."
if((Test-Path -path $LogPath) -ne $True)
{
write-host"Provide proper values to LogPath folder" -ForegroundColor Red
}
else
{
Add-Content -Path $FilePath -Value $logHeader
$script:logFileCreated = $True
write-host "Log file created..."
[string]$info = [System.String]::Format("[$Date] {0}: {1}",$label, $logMsg)
Add-Content -Path $FilePath -Value $info
}
}
else
{
[string]$info = [System.String]::Format("[$Date] {0}: {1}",$label, $logMsg)
Add-Content -Path $FilePath -Value $info
}
}
try
{
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll"
#Bind to site collection
$password = Read-Host -Prompt "Enter password" -AsSecureString
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($Adminurl)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username,$Password)
$Context.Credentials = $Creds
Write-Host "Successfully connected.." -ForegroundColor Green
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteColUrl)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username,$Password)
$ctx.Credentials = $Creds
#Retrieve lists
$web = $ctx.Web
$Users = $ctx.Web.SiteUsers
$list = $ctx.Web.Lists.GetByTitle("Test")
$query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
$items = $list.GetItems($query)
$ctx.Load($items)
$ctx.Load($Users)
$ctx.Load($web)
$ctx.Load($List)
$ctx.ExecuteQuery()
$PeopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($Context)
foreach($item in $items)
{
Foreach ($User in $Users)
{
$UserNameValue = $User.LoginName
#write-Host $UserNameValue -ForegroundColor Red
$UserProfile = $PeopleManager.GetPropertiesFor($UserNameValue)
$context.Load($UserProfile)
$Context.ExecuteQuery()
$ToolVal = $UserProfile.UserProfileProperties.Testvalue
$PeopleManager.SetSingleValueProfileProperty($UserProfile.AccountName, "Testvalue", "[2,3]")
$context.ExecuteQuery()
}
}
}
catch
{
write-host"Error: " $_.Exception.Message
}