当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 向文档库中的每个文件夹授予权限

作者:精品下载站 日期:2024-12-14 15:23:04 浏览:14 分类:玩电脑

SharePoint Online:使用 PowerShell 向文档库中的每个文件夹授予权限


要求:授予对 SharePoint Online 文档库中所有子文件夹的权限。

[玩转系统] SharePoint Online:使用 PowerShell 向文档库中的每个文件夹授予权限

如何授予文档库中所有文件夹的权限?

当您将用户添加到 SharePoint Online 中的文档库时,该用户将获得访问继承权限的所有文件夹的权限。具有独特权限的文件夹怎么样?授予权限时,请确保选中“共享此文件夹中的所有内容,甚至具有唯一权限的项目”复选框。

要向 SharePoint Online 文档库中的所有文件夹授予权限,您可以使用以下步骤:

  1. 导航到要向其中的所有文件夹授予权限的文档库。
  2. 单击右上角的齿轮图标,然后选择“文档库设置”
  3. 在“权限和管理”部分中,单击“此文档库的权限”。
  4. 单击功能区中的“授予权限”按钮。
  5. 输入您想要授予权限的人员或组的电子邮件地址或姓名,然后选择您想要授予的权限级别(例如,阅读、贡献等)。
  6. 设置“共享此文件夹中的所有内容,甚至具有唯一权限的项目。”,单击“共享”按钮授予权限。

    [玩转系统] SharePoint Online:使用 PowerShell 向文档库中的每个文件夹授予权限

  7. 这将授予文档库中所有文件夹的权限。

SharePoint Online:使用 PowerShell 向文件夹授予权限

如果您必须为每个文件夹手动执行此操作,则向 SharePoint Online 库中的所有文件夹授予权限可能会是一个乏味的过程。在这篇博文中,我们将向您展示如何使用 PowerShell 快速授予对 SharePoint Online 文档库中所有文件夹的权限。

以下是用于将用户添加到 SharePoint Online 列表或文档库中的每个文件夹的 PowerShell。


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Set Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Documents"
$UserAccount = "[email protected]"
$PermissionLevel= "Contribute"

#Function to Add User to Folder Permissions
Function Grant-SPOFolderPermission() {
    Param(
        [Microsoft.SharePoint.Client.Folder]$Folder, 
        [String]$UserAccount, 
        [String]$PermissionLevel
        )
    Try {

        #Check if Folder has unique permission already
        $Folder.ListItemAllFields.Retrieve("HasUniqueRoleAssignments")
        $Ctx.ExecuteQuery()

        If($Folder.ListItemAllFields.HasUniqueRoleAssignments -ne $true)
        {
            #Break Folder Permission inheritence - Keep all existing folder permissions & Item level permissions
            $Folder.ListItemAllFields.BreakRoleInheritance($True,$True)
            $Ctx.ExecuteQuery()
            Write-host -f Yellow "`tFolder's Permission inheritance broken..."
        }

        #Get the SharePoint User
        $User = $Ctx.Web.EnsureUser($UserAccount)
        $Ctx.load($User)
        $Ctx.ExecuteQuery()

        #Get the role required
        $Role = $Ctx.web.RoleDefinitions.GetByName($PermissionLevel)
        $RoleDB = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($Ctx)
        $RoleDB.Add($Role)
          
        #Assign permissions
        $UserPermissions = $Folder.ListItemAllFields.RoleAssignments.Add($User,$RoleDB)
        $Folder.Update()
        Write-host -f Green "`tAdded User to Folder Permissions!"
    }
    catch {
        write-host "Error in Grant Permissions: $($_.Exception.Message)" -foregroundcolor Red
    }
}

#Get Credentials to connect
$Cred = Get-Credential
  
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
    #Get the List
    $List = $Ctx.web.Lists.GetByTitle($ListName) 
    
    #Get Sub-Folders of the List
    $SubFolders = $List.RootFolder.Folders
    $Ctx.Load($SubFolders)
    $Ctx.ExecuteQuery()
     
    #Iterate through Each Sub-Folder
    ForEach($Folder in $SubFolders)
    {
        #Exclude "Forms" and Hidden folders
        If(($Folder.Name -ne "Forms") -and (-Not($Folder.Name.StartsWith("_"))))
        {
            #Get the Folder's Server Relative URL
            Write-host -f Yellow "Granting Permissions on Folder:"$Folder.Name
            Grant-SPOFolderPermission -Folder $Folder -UserAccount $UserAccount -PermissionLevel $PermissionLevel
        }
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

此脚本递归循环遍历给定列表或库中的每个子文件夹,并通过添加具有给定访问权限的给定用户来更改文件夹权限。

PnP PowerShell 将用户添加到文档库中的所有文件夹:

Set-PnPListItemPermission cmdlet 会中断项目的权限继承(如果尚未中断),并根据给定参数添加/删除权限。


#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$ListName="Documents"
$ParentFolderURL = "/Shared Documents" #Site Relative Path of the document Library
$UserAccount = "[email protected]"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get all Folders from the given location - Exclude Hidden
$AllFolders= Get-PnPFolderItem -ItemType Folder -FolderSiteRelativeUrl $ParentFolderURL | Where {($_.Name -ne "Forms") -and (-Not($_.Name.StartsWith("_")))}

#Iterate through each Folder
ForEach($Folder in $AllFolders)
{
    Write-host ("Granted Permission to '{0}' at {1} " -f $Folder.Name,$Folder.ServerRelativeUrl)
    #Grant Contribute permissions to the Folder
    Set-PnPListItemPermission -List $ListName -Identity $Folder.ListItemAllFields -User $UserAccount -AddRole 'Contribute'
} 

另一篇文章中解释了向 SharePoint Online 中的文件夹授予权限:如何使用 PowerShell 在 SharePoint Online 中授予文件夹权限?

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯