Forum Discussion
Administrer les droits des dossiers partagés SPO via powershell
Hi ViriisXP,
thanks for your question, I don't speak French so I will try to help you in English.
To achieve the permission settings you described for your SharePoint Online (SPO) folders and subfolders using PowerShell (if I translated it properly), you can use the SharePoint Online Management Shell.
Before running the script, make sure you have installed the SharePoint Online Management Shell and connected to your SharePoint Online environment.
You can download SharePoint Online Management Shell here:
Download SharePoint Online Management Shell from Official Microsoft Download Center
Here's a PowerShell script that sets the permissions for each folder and its subfolders as you mentioned:
# Connect to SharePoint Online
Connect-SPOService -Url https://yourtenantname-admin.sharepoint.com
# Set the root URL of your document library
$rootUrl = "https://yourtenantname.sharepoint.com/sites/sitename/Shared Documents"
# Function to set folder permissions
function Set-FolderPermissions {
param (
[Microsoft.SharePoint.Client.Folder]$folder,
[Microsoft.SharePoint.Client.RoleType]$permissionLevel
)
$ctx = $folder.Context
$folder.ListItemAllFields.BreakRoleInheritance($true, $false)
$ctx.Load($folder.ListItemAllFields.RoleAssignments.Add($folder.ParentList.ParentWeb.SiteGroups.GetByName("YourReadOnlyGroup"), $permissionLevel))
$folder.Context.ExecuteQuery()
}
# Function to recursively set permissions for folders and subfolders
function Set-PermissionsRecursively {
param (
[Microsoft.SharePoint.Client.Folder]$folder
)
$ctx = $folder.Context
$ctx.Load($folder.Folders)
$ctx.Load($folder.ListItemAllFields)
$ctx.ExecuteQuery()
# Set permission for the current folder
Set-FolderPermissions -folder $folder -permissionLevel [Microsoft.SharePoint.Client.RoleType]::Read
# Recursively set permissions for subfolders
foreach ($subfolder in $folder.Folders) {
Set-PermissionsRecursively -folder $subfolder
}
}
# Main script
$ctx = Get-SPOContext
# Get the root folder
$rootFolder = $ctx.Web.GetFolderByServerRelativeUrl($rootUrl)
$ctx.Load($rootFolder)
$ctx.ExecuteQuery()
# Set permissions recursively for each folder and its subfolders
Set-PermissionsRecursively -folder $rootFolder
You will need to modify the script by replacing `https://yourtenantname.sharepoint.com/sites/sitename/Shared Documents` with the actual URL of your document library. Also, replace `"YourReadOnlyGroup"` with the name of the security group that should have read-only access.
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.
If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic
- mlarrouyJul 25, 2023Copper Contributor
Bonjour LeonPavesic (désolé je réponds avec mon compte pro), il semblerait si je comprends bien, que chaque dossiers disposera des mêmes droit que son parent ?
Ce que je souhaite c'est que
- Si le dossier n'a plus de sous-dossier, alors les droits attribués sont "Modifier".
- Si le dossier a un sous-dossier, alors les droits attribués sont "Lecture" (+ recursive en effet) .
Si je reprends l'exemple de la capture :
S1 Lecture S1D1 Modification S1D2 Modification S1D3 Modification S1D4 Modification .... .... S2 Lecture S2D1 Lecture S2D1SSD1 Modification S2D1SSD2 Modification .... .... S2D1SSD5 Lecture S2D1SSD5E1 Modification S2D1SSD5E2 Modification ... ... Je suis vraiment désolé si je ne suis pas assez clair. 😕
Merci énormément pour l'aide ! Thx