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

[玩转系统] 如何使用 PowerShell 将权限从 SharePoint Online 中的一个列表复制到另一个列表?

作者:精品下载站 日期:2024-12-14 21:31:37 浏览:13 分类:玩电脑

如何使用 PowerShell 将权限从 SharePoint Online 中的一个列表复制到另一个列表?


要求:将 SharePoint Online 中的权限从一个列表复制到另一个列表。

PowerShell 在 SharePoint Online 列表和库之间复制权限

您是否曾经需要在 SharePoint Online 中将权限从一个列表复制到另一个列表?也许您已经设置了新的列表或库,并且需要快速向用户和组授予与现有列表相同的权限。您可能知道,如果不使用第 3 方工具,就无法将权限从现有列表克隆到 SharePoint Online 中的新列表,因为没有任何内置方法可以执行此操作。好吧,不再是了! PowerShell 可以帮助使该过程变得更加容易。此博文将向您展示如何使用 PowerShell 将权限从 SharePoint Online 中的一个列表复制到另一个列表。

[玩转系统] 如何使用 PowerShell 将权限从 SharePoint Online 中的一个列表复制到另一个列表?

以下是用于将权限从 SharePoint Online 中的一个列表复制到另一个列表的 PowerShell 脚本:


#PowerShell Function to copy permissions between Lists in SharePoint
Function Copy-PnPListPermissions
{
    [cmdletbinding()]
     param(
         [Parameter(Mandatory=$True)] [string] $WebURL,
         [Parameter(Mandatory=$True)] [string] $SourceListName,
         [Parameter(Mandatory=$True)] [string] $TargetListName,
         [Parameter(Mandatory=$False)] [Bool] $AppendToExisting = $True
     )
    Try {
        #Connect to PnP Online
        Connect-PnPOnline -Url $WebURL -Interactive

        #Get the Web
        $Web = Get-PnPweb
        $Ctx = Get-PnPContext

        #Get Source and Target Lists
        $SourceList = Get-PnPList $SourceListName  -Includes HasUniqueRoleAssignments -ThrowExceptionIfListNotFound
        $TargetList = Get-PnPList $TargetListName  -Includes HasUniqueRoleAssignments -ThrowExceptionIfListNotFound

        #if permissions are Inherited in Target List, Break the Inheritance
        If(!$TargetList.HasUniqueRoleAssignments)
        {
            If($AppendToExisting -eq $True)
            {
                Set-PnPList -Identity $TargetList -BreakRoleInheritance -CopyRoleAssignments
            }
            else
            {
                Set-PnPList -Identity $TargetList -BreakRoleInheritance
            }
        }
        Else #If the List has unique Permissions already
        {
            If($AppendToExisting -eq $False)
            {
                Set-PnPList -Identity $TargetList -ResetRoleInheritance
                Set-PnPList -Identity $TargetList -BreakRoleInheritance
            }
        }

        #Get all users and group permissions assigned to the source object
        $SourceRoleAssignments = Get-PnPProperty -ClientObject $SourceList -Property RoleAssignments
 
        #Copy Source list permissions to Destination List
        ForEach($RoleAssignment in $SourceRoleAssignments)
        {
            #Get RoleDefinitions of the Role Assignment
            Get-PnPProperty -ClientObject $RoleAssignment -Property RoleDefinitionBindings, Member

            #Leave the Hidden permissions
            If($RoleAssignment.Member.IsHiddenInUI -eq $False)
            {
                $SourcePermissions = $RoleAssignment.RoleDefinitionBindings | Where {$_.Name -notin("Limited Access")}
                $PermissionLevels = ($SourcePermissions | Select -ExpandProperty Name) -join "; "

                If($SourcePermissions -ne $null)
                {
                    #Grant Source List's Permission Level to the Target List
                    $RoleDefBindings = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($Ctx)
                    ForEach($RoleDefinition in $SourcePermissions)
                    {
                        $RoleDefBindings.Add($RoleDefinition)
                    }
                    $Permissions = $TargetList.RoleAssignments.Add($RoleAssignment.Member,$RoleDefBindings)
                    $TargetList.Update()
                    Invoke-PnPQuery
                    Write-host "Copied '$($RoleAssignment.Member.Title)' with Permissions '$PermissionLevels'"
                }
            }
        }
    }
    Catch {
        write-host -f Red "Error Copying List Permissions!" $_.Exception.Message
    }
}

#Set Parameters
$WebURL = "https://crescent.sharepoint.com/sites/Marketing"
$SourceListName = "Documents"
$TargetListName = "Migration"

#Call the function to copy list permissions 
Copy-PnPListPermissions -WebURL $WebURL -SourceListName $SourceListName -TargetListName $TargetListName

默认情况下,此脚本会附加到目标列表的现有权限。如果要清除目标列表的所有现有权限并从源列表复制权限,可以为 -AppendToExisting 传递一个可选参数“$False”。

以下是我关于在 SharePoint Online 中复制权限的其他帖子:

  • 如何使用 PowerShell 将权限从 SharePoint Online 中的一个文件夹复制到另一个文件夹?
  • 如何使用 PowerShell 在 SharePoint Online 中将权限从一个用户复制到另一个用户?
  • SharePoint Online:使用 PowerShell 克隆用户组成员身份

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

取消回复欢迎 发表评论:

关灯