1

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 = "firsttName.lastname@siteoffice.com" 
   $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
       }
2

I figured it out by setting the RequestTimeOut property of Context to millisecond

$context.RequestTimeOut = 5000*10000
1

STEP 1 Click the "Start" button from the desktop and then click the "All Programs" option.

STEP 2 Click the "Accessories" option and then click the "Windows PowerShell" option. The PowerShell program launches on your screen.

STEP 3 Enter "Dir WSMan:\localhost\shell" into the command line and then press the "Enter" key on your keyboard.

STEP 4 View the list of configuration settings and look for the "IdleTimeout" field.

STEP 5 Enter "Set-Item WSMan:\localhost\shell\IdleTimeout" into the command line, and then press the "spacebar" key.

STEP 6 Enter the number, in milliseconds, that determines how long a session is disconnected before it is disregarded.

STEP 7 Press the "Enter" key and then the new timeout setting is configured.

Credit: Greg Lindberg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.