Unable to connect sharepoint online site collection using powershell

Copper Contributor

I have to upload the CSV file data to the SharePoint list using Powershell, here the connection is established up to the admin site. but it is not accessing the Sharepoint site collection, showing an error like as:

Connect-SPOService : Could not authenticate to SharePoint Online https://siteurl(here I am providing actuall site url, till site name)using OAuth 2.0
At line:1 char:1
+ Connect-SPOService
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Connect-SPOService], AuthenticationException
+ FullyQualifiedErrorId : Microsoft.Online.SharePoint.PowerShell.AuthenticationException,Microsoft.Online.SharePoint.PowerShell.ConnectSPOService

4 Replies
Have you tried to use the PNP Module?

@Lokeswar_Reddy 

 

This is expected error since you need to pass the Admin site URL (Tenant site url) for Connect-SPOService.

 

You can check this post : https://docs.microsoft.com/en-us/powershell/module/sharepoint-online/connect-sposervice

 

 

Connect-SPOService -url https://TenantName-admin.sharepoint.com

 

 

As you know, the Connect-SPOService cmdlet is belong to SPO Management module. For List items update, you have to either use CSOM based script or PnP PowerShell commands.

 

 

$SiteUrl = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "CustomerContacts"
$CSVPath = "C:\temp\CustomerContacts.csv"
 
#Get CSV file content
$CSVData = Import-CsV -Path $CSVPath
 
#Connect to site
Connect-PnPOnline $SiteUrl -Interactive
 
#Iterate through each Row in the CSV and import as list item SPO List
ForEach ($Row in $CSVData)
{
#Add List Items
Add-PnPListItem -List $ListName -Values @{
"CustomerName" = $($Row.CustomerName);
"Email" = $($Row.Email); 
"Mobile" = $($Row.Mobile);
};
}

 

 

 

 

Thanks for your reply,
Actually, I do not see any error while connecting to the admin site, only here this issue is connecting to the Sharepoint site collection.
please let me know about this issue.

@Kevin Morgan 

 

The point is: You can't connect the individual site collection using this command (Connect-SPOService ). The command is designed to connect only with admin site. Once you connected the Admin site, you can use the following command to get specific site details.

 

 

Get-SPOSite -Identity https://contoso.sharepoint.com

 

 

And you can use the commands listed in the below post to manage SPO sites.

 

https://docs.microsoft.com/en-us/powershell/module/sharepoint-online/?view=sharepoint-ps

 

You can use these commands only to manage SPO sites and I believe, you can't manage specific list or list items using these commands. So, you have to either use PnP PowerShell or CSOM based PowerShell.