Búsqueda de sitios web

¿Cómo copiar permisos de una lista a otra en SharePoint Online usando PowerShell?


Requisito: Copiar permisos de una lista a otra en SharePoint Online.

PowerShell para copiar permisos entre listas y bibliotecas de SharePoint Online

¿Alguna vez ha necesitado copiar permisos de una lista a otra en SharePoint Online? Tal vez haya configurado una nueva lista o biblioteca y necesite otorgar rápidamente a los usuarios y grupos los mismos permisos que una lista existente. Es posible que sepa que no hay formas de clonar permisos de una lista existente a una lista nueva en SharePoint Online sin utilizar herramientas de terceros, ya que no existe ninguna forma integrada de hacerlo. Bueno, ¡ya no! PowerShell puede ayudar a que el proceso sea mucho más sencillo. Esta publicación de blog le mostrará cómo copiar permisos de una lista a otra en SharePoint Online usando PowerShell.

Aquí está el script de PowerShell para copiar permisos de una lista a otra en SharePoint Online:


#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

Este script se agrega a los permisos existentes de la lista de objetivos de forma predeterminada. Puede pasar un parámetro opcional para -AppendToExisting con “$False” si desea borrar todos los permisos existentes de la lista de destino y copiar los permisos de la lista de origen.

Aquí están mis otras publicaciones sobre permisos de copia en SharePoint Online:

  • ¿Cómo copiar permisos de una carpeta a otra en SharePoint Online usando PowerShell?
  • ¿Cómo copiar permisos de un usuario a otro en SharePoint Online usando PowerShell?
  • SharePoint Online: clonar membresías de grupos de usuarios usando PowerShell

Artículos relacionados