sharpoint PnP powershell Get-PnPTenantSite

Pfeiffer Andreas - UCI 245 Reputation points
2024-03-20T11:17:08.6+00:00

I have a powershell script (version 7) that at some point the cmdlt : Get-PnPList throws me an error : Attempted to perform an unauthorized operation.

I connect my script as follow :

Connect-PnPOnline -Url $SiteURL -ClientId <clientId> -Tenant <tenant>.onmicrosoft.com -Thumbprint <thumbprint>

Note that the $SiteURL value is : https://<tenant>-admin.sharepoint.com

and then do :

$FileData = @()
$DocumentLibraries = Get-PnPList -> on error

The permission on my apps is Sites.FullControl.All

Why do I have this error while I expected that my apps has full control on all sites ? Should I also add an app role ? If yes, how can I achieve this, especially what would be the value ?

My goal is to get a report on file sizes in all sharepoints sites. Strangely, when I select a single site it is working. But I'd like my script to loop in all sharepoints site files.

If somebody has an idea, thank you in advance

Microsoft 365 and Office | SharePoint | For business | Windows
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Ling Zhou_MSFT 23,620 Reputation points Microsoft External Staff
    2024-03-21T05:54:51.3733333+00:00

    Hi @Pfeiffer Andreas - UCI,

    Thank you for posting in this community.

    Based on the description of your problem, I ran a test. I only added Sites.FullControl.All permission. Then successfully connected to SharePoint Online. The Get-PnPList command also did not receive any error.

    User's image

    Screenshot 2024-03-21 104510 We need to narrow down the problem and try some things. So, please kindly provide more information below to let us work further.

    1.Please follow this article to see if you are configuring the Azure AD App correctly. The main thing is to make sure your certificate is configured correctly.

    Connect to SharePoint Online using Azure AD App ID from PowerShell

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link. 

    2.Since there will be a large number of document libraries in a tenant, we need to iterate through each document library in each site collection and all of its sub-sites to get the file sizes. This operation leads to a lot of loops and consumes a long time. Therefore, it is more appropriate to get the size of the files in all the file libraries in a site. Perhaps this is what is causing your problem.

    But you can try combining the following two Power Shells if you want to get the file sizes of all the file libraries in the tenant at once.

    PnP PowerShell to Get All Document Libraries in SharePoint Online

    #Set Variables
    $SiteURL = "https://crescent.sharepoint.com/sites/marketing"
      
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
     
    #Get all document libraries - Exclude Hidden Libraries
    $DocumentLibraries = Get-PnPList | Where-Object {$_.BaseTemplate -eq 101 -and $_.Hidden -eq $false} #Or $_.BaseType -eq "DocumentLibrary"
     
    #Get Document Libraries Name, Default URL, Number of Items,ID
    $DocumentLibraries | Select Title, DefaultViewURL, ItemCount, ID
    

    Get File Size for all documents in a SharePoint Online Document Library

    #Parameters
    $SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
    $ListName= "Branding"
    $ReportOutput = "C:\Temp\FileSizeRpt.csv"
       
    #Connect to SharePoint Online site
    Connect-PnPOnline $SiteURL -Interactive
     
    #Array to store results
    $Results = @()
       
    #Get all Items from the document library
    $List  = Get-PnPList -Identity $ListName
    $ListItems = Get-PnPListItem -List $ListName -PageSize 500 | Where {$_.FileSystemObjectType -eq "File"}
    Write-host "Total Number of Items in the List:"$List.ItemCount
     
    $ItemCounter = 0
    #Iterate through each item
    Foreach ($Item in $ListItems)
    {
            $Results += New-Object PSObject -Property ([ordered]@{
                FileName          = $Item.FieldValues.FileLeafRef
                RelativeURL       = $Item.FieldValues.FileRef
                FileSize          = $Item.FieldValues.File_x0020_Size
                TotalFileSize     = $Item.FieldValues.SMTotalSize.LookupId
            })
        $ItemCounter++
        Write-Progress -PercentComplete ($ItemCounter / ($List.ItemCount) * 100) -Activity "Processing Items $ItemCounter of $($List.ItemCount)" -Status "Getting data from Item '$($Item['FileLeafRef'])"
    }
      
    #Export the results to CSV
    $Results | Export-Csv -Path $ReportOutput -NoTypeInformation
    Write-host "File Size Report Exported to CSV Successfully!"
    

    If the answer is helpful, please click __"Accept 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.


1 additional answer

Sort by: Most helpful
  1. Pfeiffer Andreas - UCI 245 Reputation points
    2024-03-20T14:46:12.96+00:00

    Ok got there. The permissions I gave to my apps is :

    User's image

    Having said that, I'd like to understand the difference between 1 and 2.

    thank you

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.