2

I'm facing this issue while running below script for SharePoint list. Previously same script was working absolutely fine but now I'm getting this error. Any help to resolve this will be appreciated.

#Load SharePoint CSOM Assemblies
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
       
    #Variables for Processing
    $SiteUrl = "https:siteurl/sites/query-uat"
    $ListName="Query"
    $BatchSize = 500
    
    $Cred = Get-Credential
    $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
    
     Try {
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Cred
      
        #Get the web and List
        $Web=$Ctx.Web
        $List=$web.Lists.GetByTitle($ListName)
        $Ctx.Load($List)
        $Ctx.ExecuteQuery()
        Write-host "Total Number of Items Found in the List:"$List.ItemCount
     
        $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
        $Query.ViewXml = "<View Scope='RecursiveAll'><Query><OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy></Query><RowLimit Paged='TRUE'>$BatchSize</RowLimit></View>"
     
        Do {  
            #Get items from the list in batches
            $ListItems = $List.GetItems($Query)
            $Ctx.Load($ListItems)
            $Ctx.ExecuteQuery()
             
            $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
            Write-host Updating $($ListItems.count) Items Starting from Item ID $ListItems[0].ID
     
            #Loop through each item and update
            ForEach($item in $ListItems)
            {
                if ($item["QueryStatus"] -eq "Assigned to CMT ")
                {
                    $item["QueryStatus"] = "Assigned to DUMU "
                    $item.Update()
                    
                }
                elseif ($item["QueryStatus"] -eq "Assigned to CMT ")
                {
                    $item["QueryStatus"] = "Assigned to DUMU "
                    $item.Update()
                    
                }
            
                elseif ($item["QueryStatus"] -eq "Assigned")
                {
                    $item["QueryStatus"] = "Assigned to"
                    $item.Update()
                    
                }
                elseif ($item["QueryStatus"] -eq "Assigned to BMT ")
                {
                    $item["QueryStatus"] = "Assigned to DUMU "
                    $item.Update()
                    
                }
            
                elseif ($item["QueryStatus"] -eq "Assigned to IMT ")
                {
                    $item["QueryStatus"] = "Assigned to BDX Mgt "
                    $item.Update()
                    
                }
                elseif ($item["QueryStatus"] -eq "Assigned to IMT ")
                {
                    $item["QueryStatus"] = "Assigned to BDX Mgt "
                    $item.Update()
                
                }
           } 
           $Ctx.ExecuteQuery()  
           Write-host "Item updated Successfully"  -foregroundcolor Green   
     
        } While ($Query.ListItemCollectionPosition -ne $null)
     
        Write-host -f Green "All Items updated!"
    }
    Catch {
        write-host -f Red "Error u List Items!" $_.Exception.Message
    }

1 Answer 1

4

I was having the same issue with a script connecting to SharePoint. I believe it is from the TLS settings for the PowerShell environment I was running my script from not being compatible with the minimum requirements on the SharePoint side. In other words, it is probably trying to use TLS 1.0 or 1.1 (insecure/broken) instead of 1.2 or 1.3 (secure). I added this before any web connection to SharePoint in my script as I have experienced similar issues with web connections in other PowerShell scripts:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

Once I added that my error went away. So, for your script, I would start with just adding it as the first line in your Do statement:

Do { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 ... }

You may need to add it in other places as well.

3
  • Thank you, Now it's working fine. I have added above line also in IE browser -> settings -> Internet option -> advance setting - TLS 1.1 & 1.2 selected then it working fine now. Apr 21, 2021 at 10:19
  • Glad to hear! Please accept my answer:) thanks
    – ragenkagen
    Apr 21, 2021 at 14:30
  • Worked for me too. Thanks!
    – jerom987
    Jun 11, 2021 at 11:40

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