Exception calling ExecuteQuery with 0 argument(s): Unable to connect to the remote server error in PowerShell SharePoint Online

Recently, I was trying to insert items to the SharePoint online list through the PowerShell command. The error came in the below line while calling the ExecuteQuery() method. The error comes as an Exception calling “ExecuteQuery” with “0” argument(s): “Unable to connect to the remote server”

$Context.ExecuteQuery().

Exception calling “ExecuteQuery” with “0” argument(s): “Unable to connect to the remote server”

You can write in Windows PowerShell ISE, or you can also use Visual Studio Code to write and debug PowerShell Script.

Exception calling ExecuteQuery with 0 argument(s) Unable to connect to the remote server
Exception calling ExecuteQuery with 0 argument(s) Unable to connect to the remote server

Exception calling ExecuteQuery with 0 argument(s): Unable to connect to the remote server

The below code I was writing to connect to the SharePoint Online site from PowerShell.

$SiteURL = "https://onlysharepoint2013.sharepoint.com/sites/Bhawana/"
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$securePassword=ConvertTo-SecureString "MyPassword" -AsPlainText -Force
$Context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials("bijay@XXXXX.onmicrosoft.com", $securePassword)

The username and password everything was working fine. The same code was also working on another server.

Then we saw the internet was not working on the server. We tried to open the site in a browser, where it did not open also. Once the internet was up, the code worked as expected.

ExecuteQuery” With “0” Argument(S): “The Remote Server Returned An Error: (403) Forbidden

You might also get an error like ExecuteQuery” With “0” Argument(S): “The Remote Server Returned An Error: (403) Forbidden while working with PowerShell SharePoint Online Office 365.

You can solve by adding Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll explicitly in the PowerShell script.

Import-Module “E:\Bijay\Microsoft.SharePoint.Client.dll”
Import-Module “E:\Bijay\Microsoft.SharePoint.Client.Runtime.dll”

Sometimes, even if you install the SharePoint Online management shell, you might get the above issue.

Exception calling “executequery” with “0” argument(s): the remote server returned an error: (403) forbidden

If you are still Exception calling “executequery” with “0” argument(s): the remote server returned an error: (403) forbidden error, you can try the below things.

  • Open PowerShell ISE in administrator mode
  • The user account should be an admin account

I hope this will help to resolve the Exception calling ExecuteQuery with 0 argument(s): Unable to connect to the remote server error in PowerShell SharePoint.

You may also like:

>