How do I access SharePoint with PowerShell 7?

Limosan 0 Reputation points
2024-11-22T07:45:39.88+00:00

Hello everyone,

I'm trying to create a PS script that reads the set permissions of a folder from SharePoint and creates a copy of it with the same rights.

In MS Entra Admin Center, I've created a new app and granted it numerous permissions for testing purposes.

My PS7 script looks like this:

$clientId = "[...]"
$clientSecret = "[...]"#Value, not the ID 
$tenantId = "[...]" 
$siteUrl = "[...].sharepoint.com/sites/ExterneKundendaten" 
$libraryName = "Dokumente" 
$folderPath = "_Unsortiert" 
try { 
$connection = Connect-PnPOnline -Url $siteUrl -ClientId $clientId -ClientSecret $clientSecret Write-Host "Connection successfully established" 
} 
catch { Write-Host "Connection error: $_"}

I'm getting the following error message:

Connection error: The remote server returned an error: (401) Unauthorized.

For testing, I've also tried the following connection setup:

$clientSecretSecure = ConvertTo-SecureString $clientSecret -AsPlainText -Force
Connect-PnPOnline -Url $siteUrl -ClientId $clientId -ClientSecret $clientSecretSecure

Connect-PnPOnline -Url $siteUrl -ClientId $clientId -ClientSecret $clientSecret 
Connect-PnPOnline -Url $siteUrl -ClientId $clientId -ClientSecret $clientSecret -Tenant $tenantId 

$clientSecretSecure = ConvertTo-SecureString $clientSecret -AsPlainText -Force 
Connect-PnPOnline -Url $siteUrl -ClientId $clientId -ClientSecret $clientSecretSecure -Tenant $tenantId 

Unfortunately, all without success.

This is roughly how the later process should be, but it's already stuck at point 1

1. Establish connection to SharePoint Online
2. Define library name and folder path
$libraryName = "Dokumente" # Name of the document library 
$folderPath = "_Unsortiert" # Path to the folder 
3. Get the list item for the folder
$folderItem = Get-PnPListItem -List $libraryName -FolderServerRelativeUrl "/sites/ExterneKundendaten/$libraryName/$folderPath" 
4. Get the role assignments for the folder
$roleAssignments = Get-PnPRoleAssignment -List $libraryName -Identity $folderItem.Id 
5. Display permissions
$roleAssignments | Format-Table Principal, RoleDefinitionBindings 
6. Disconnect
Disconnect-PnPOnline 

PSVersion 7.4.6

PnP.PowerShell 2.12.0

Does anyone have an approach on how I can proceed and/or can someone recommend a good (and current!!) tutorial?

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

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2024-11-22T08:58:46.1966667+00:00

    Hi @Limosan,

    This issue usually occurs you didn't grant correct permission. Please grant the new permission for the app and set correct scope on https://contoso.sharepoint.com/sites/sitename/_layouts/15/appinv.aspx

    And the App Permission XML should be like following

    <AppPermissionRequests AllowAppOnlyPolicy="true"> <AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="FullControl" /> </AppPermissionRequests>
    

    Here is the document for more details

    https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs


    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.


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.