A connection error occurs in powershell

Kuwada Hiroyuki--ITSL900 0 Reputation points
2024-07-10T01:00:41.9566667+00:00

I am using powershell to connect to SharePoint Online on a server (Windows Server 2016 Datacenter).

The batch has been running since 2021, but since June of this year, I have been getting the error "An exception occurred while calling "ExecuteQuery": Unable to connect to remote server."

It has been running for a long time, so I don't think it's a problem with the program, but if you have any idea what the cause of the error might be, could you please let me know?

I have also confirmed that the batch ends normally when run on a work PC.

Source code excerpt


$objPassword = ConvertTo-SecureString $strPassword -AsPlainText -Force;

$objCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($strUser, $objPassword);

$objCtx = New-Object Microsoft.SharePoint.Client.ClientContext($strSiteUrl);

$objCtx.Credentials = $objCredentials;

$objSite = $objCtx.Site;

$objCtx.Load($objSite);

$objCtx.Load($objSite.RootWeb);

$objCtx.ExecuteQuery(); ←This is where the error occurs.


SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,211 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yanli Jiang - MSFT 23,581 Reputation points Microsoft Vendor
    2024-07-10T07:14:27.18+00:00

    Hi @Kuwada Hiroyuki--ITSL900

    There are many reasons for this situation. Let's do some troubleshooting to narrow down the scope of the problem.

    According to your description, the batch can run successfully on the work PC, which means that there is no problem with the batch, the SharePoint site, and the account verification. Then there should be a problem with other settings of the server running the batch. Please check the following:

    1, Network Configuration:

    Network Connection: Check whether the network connection of the server is normal.

    Firewall/Proxy Changes: Ensure there haven't been any recent changes to your network firewall or proxy settings that could be blocking access to SharePoint Online.

    2, PowerShell Module Versions:

    Module Updates: Ensure that your PowerShell modules for SharePoint Online are up to date.

    3, TLS/SSL Protocols:

    TLS Version: SharePoint Online requires TLS 1.2. Ensure that your server is configured to use TLS 1.2. You can add the following code at the beginning of your script to enforce TLS 1.2:

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

    4, Windows Updates: Make sure your server has the latest updates installed. Sometimes, Windows Server component updates can affect network connectivity and protocols.

    5, Logs: Review the Windows Event Viewer logs around the time when the error occurs. Network-related or .NET framework-related issues might be logged there.

    Reference:

    https://learn.microsoft.com/en-us/answers/questions/940461/error-while-connecting-to-sharepoint-online-using

    Hope this can help.


    If the answer is helpful, please click "Accept as Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments