Búsqueda de sitios web

Agregar o quitar permisos a la lista de SharePoint usando PowerShell


Requisito: Establecer permisos de lista en SharePoint usando PowerShell

PowerShell se puede utilizar para agregar o quitar permisos a la lista de SharePoint. Aquí está mi script de PowerShell para otorgar y eliminar permisos a sitios, listas y bibliotecas de SharePoint.

Otorgar permiso a un usuario o grupo para la lista de SharePoint

¿Necesita darle acceso rápidamente a alguien a su lista de SharePoint? ¡Puede otorgar permiso a un usuario en SharePoint usando PowerShell! En esta publicación de blog, lo guiaremos a través del script que otorga permisos a un usuario o grupo:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Function to Grant Permission to List
function Grant-PermissionToList($WebUrl, $ListName, $UserAccount, $PermissionLevel)
{
    #Get Web and List objects
    $Web = Get-SPWeb -Identity $WebUrl
    $List = $web.Lists.TryGetList($ListName)

    if ($List -ne $null)
    {
        #We must break inheritance to grant permission directly on the list
        if ($List.HasUniqueRoleAssignments -eq $False)
        {
            $list.BreakRoleInheritance($True)
        }

        #Get the user object
        $User = $web.EnsureUser($UserAccount)
        #FOR GROUPS use: $group = $web.SiteGroups[$GroupName]
        #$assignment = new-object Microsoft.SharePoint.SPRoleAssignment($group)
            
        #Get the permission level
        $role = $web.RoleDefinitions[$PermissionLevel]
        $assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($User)
        $assignment.RoleDefinitionBindings.Add($role) 
        $list.RoleAssignments.Add($assignment)
        $list.Update()

        Write-Host "Granted permission $($PermissionLevel) to $($UserAccount) in list $($ListName)." -foregroundcolor Green        
    }
    $web.Dispose()
}

#Call the function to grant access to a list
Grant-PermissionToList "https://sharepoint.crescent.com" "Documents" "Global\Auditors" "Contribute"                

Script de PowerShell para eliminar permisos de la Lista:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Function to Remove Permission from List
function Remove-PermissionFromList($WebUrl, $ListName, $GroupName, $PermissionLevel)
{
    #Get Web and List objects
    $Web = Get-SPWeb -Identity $WebUrl
    $List = $web.Lists.TryGetList($ListName)

    if ($List -ne $null)
    {
        #We must break inheritance to remove permission directly from the list
        if ($List.HasUniqueRoleAssignments -eq $False)
        {
            $list.BreakRoleInheritance($True)
        }

        #Get the Group or user object
        $group = $web.SiteGroups[$GroupName]
        
        if($group -ne $null)
        {
            #For User, use: $User = $web.EnsureUser($UserAccount)
            #To Remove All permissions of the group, use: 
            #$list.RoleAssignments.Remove($group)    

             #If group doesn't has access to the given list, it triggers an error! So, lets handle it.
             try
             {
                #Set the Error Action
                $ErrorActionPreference = "Stop"
                #Get the permission level
                $role = $web.RoleDefinitions[$PermissionLevel]
                $assignment = $list.RoleAssignments.GetAssignmentByPrincipal($group)
                #Remove the permissions
                $assignment.RoleDefinitionBindings.Remove($role)
                $assignment.Update()

                $list.Update()
                Write-Host "Removed permission $($PermissionLevel) of $($GroupName) from list $($ListName)." -foregroundcolor Green

             }
             catch [ArgumentException] 
             {
                Write-Host "Group ($GroupName) doesn't has access on $($ListName)!" -ForegroundColor Red
             }
             finally
             {
                #Reset the Error Action to Default
                $ErrorActionPreference = "Continue"
             }            
        }
        else
        {
            Write-Host "Cannot find Group Name: $($GroupName) in site $($WebUrl)." -foregroundcolor red
        }        
    }
    $web.Dispose()
}

#Call the function 
Remove-PermissionFromList "https://Sharepoint.crescent.com/sites/sales" "Sales Documents" "Sales Members" "Edit"              

Aunque este script muestra cómo agregar permisos de eliminación a los objetos de la lista de SharePoint, esto también se puede utilizar en los lugares del objeto Sitio, Carpeta y Elemento de la lista. Simplemente reemplace el objeto $list con otros objetos como web, carpeta o elemento de lista. Puede otorgar o quitar permiso a un usuario en particular, a un grupo de SharePoint o a un grupo de seguridad del directorio activo.

Para agregar un usuario al grupo de SharePoint con PowerShell, consulte: ¿Cómo agregar un usuario al sitio de SharePoint usando PowerShell?

Artículos relacionados