0

I need to script all documents in a site library. I am site admin on each of the sites in the site collection but I am not site collection admin/SharePoint tenant admin.

I tried with the Sharepoint online module for PS first, but it looks like I would have needed tenant admin. Then I tried with CSOM and PnP, but none of them worked.

Can you provide a PS script or link in order to connect to my site (while being just site admin not site collection admin), in order to pick up libraries/lists etc from there?

The last script that I used :

Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" <br/>
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

$SiteUrl="https://contoso.sharepoint.com/sites/MyRootSiteName" 

#credential 
$UserName="emailAddress@Contoso.com" 
$Password ="Passwrd"
 
#Create Credential object from given user name and password 
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
 
#Set up the context 
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Ctx.Credentials = $Cred 
#Get the Web Object 
$Web = $Ctx.web 
$Ctx.Load($Web) 
$Ctx.ExecuteQuery()

The error that I got at the last line is:

Exception calling "ExecuteQuery" with "0" argument(s): "The sign-in name or password does not match one in the Microsoft account system."

Please let me know if I can connect to SP online via PS as only site admin and be able to script libary files.

0

It is possible to connect to a SharePoint Online site as site admin only. As to the error "The sign-in name or password does not match one in the Microsoft account system.", this may be caused by that your account is MFA (Multi-factor authentication) enabled. If yes, either MFA must be turned OFF or you have to follow the methods described in How to Connect to SharePoint Online with MFA-enabled accounts from PowerShell. To connect to SharePoint Online through CSOM PowerShell script with a Multi-factor authentication enabled account, use this PowerShell:

 $SiteURL = "https://contoso.sharepoint.com/sites/MyRootSiteName/MySubSiteName"
 
 #Setup Authentication Manager
 $AuthenticationManager = new-object OfficeDevPnP.Core.AuthenticationManager
 $Ctx = $AuthenticationManager.GetWebLoginClientContext($SiteUrl)
 $Ctx.Load($Ctx.Web)
 $Ctx.ExecuteQuery()

I want to confirm with you what do you mean by "script libary files" exactly? Do you want to list all files names in a Document Library using PowerShell? If yes, take a reference to this article: SharePoint Online: PowerShell to List All Files Inventory in a Document Library. Replace the code which connects you to SharePoint Online with the code above.

2
  • the code worked. thank you Allen_MSFT !
    – sehor
    2 days ago
  • Thanks for your reply :) Glad to hear that your issue has been resolved. Please don't forget to accept answer if my answer is helpful to you.
    – Allen_MSFT
    2 days ago

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.