Tìm kiếm trang web

SharePoint Online: Xóa quyền của người dùng khỏi tất cả các mục danh sách bằng PowerShell


Yêu cầu: Xóa người dùng khỏi tất cả các quyền đối với mục trong danh sách.

Làm cách nào để xóa quyền của người dùng khỏi tài liệu hoặc mục trong SharePoint Online?

Khi người dùng không còn yêu cầu quyền truy nhập vào tài liệu trong thư viện tài liệu nữa, có thể cần phải loại bỏ quyền của họ. Để loại bỏ quyền của người dùng khỏi tài liệu trong SharePoint Online, bạn có thể làm theo các bước sau:

  1. Đi tới trang SharePoint Online của bạn và dẫn hướng đến thư viện tài liệu nơi tài liệu được lưu trữ.
  2. Nhấp chuột phải vào tài liệu và chọn “Quản lý quyền truy cập” từ menu ngữ cảnh.
  3. Trong cửa sổ bật lên “Quản lý quyền truy cập”, nhấp vào menu thả xuống nhỏ bên cạnh tên người dùng và chọn “Dừng chia sẻ”. Điều này sẽ loại bỏ người dùng khỏi tài liệu cụ thể.

  4. Bạn cũng có thể nhấp vào liên kết “Nâng cao” trong cửa sổ bật lên “Quản lý quyền truy cập” >> nhấp vào “Dừng kế thừa quyền” >> Từ danh sách người dùng, chọn người dùng có quyền mà bạn muốn xóa.
  5. Nhấp vào nút “Xóa quyền của người dùng” để xóa người dùng.

  6. Xác nhận hành động bằng cách nhấp vào “OK”.
  7. Lưu các thay đổi bằng cách nhấp vào nút "Lưu".

Bằng cách làm theo các bước này, bạn có thể loại bỏ quyền của người dùng khỏi một tài liệu cụ thể trong SharePoint Online. Thực hiện việc này theo cách thủ công cho từng tài liệu có thể tốn thời gian, đặc biệt nếu có nhiều tài liệu. Hãy thảo luận các bước để loại bỏ quyền của người dùng khỏi tất cả tài liệu trong thư viện tài liệu trong SharePoint Online bằng PowerShell.

SharePoint Online: Xóa quyền của người dùng khỏi tất cả các mục bằng PowerShell

Có nhiều mục có quyền duy nhất trong danh sách SharePoint Online mà một người dùng cụ thể có quyền truy cập. Có yêu cầu xóa người dùng cụ thể khỏi tất cả các mục danh sách mà người đó có quyền truy cập. Đây là PowerShell dành cho SharePoint Online để xóa quyền cấp mục:


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

##Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/marketing"
$ListName= "Migration Documents"
$UserAccount = "i:0#.f|membership|salaudeen@crescent.com"

#Get Credentials to connect
$Cred= Get-Credential
  
#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) 
$Context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
    
#Get the List
$List = $Context.web.Lists.GetByTitle($ListName)

#Get the User
$User = $Context.Web.EnsureUser($UserAccount) 
$Context.Load($User)
$Context.ExecuteQuery()
 
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "<View Scope='RecursiveAll'><RowLimit>2000</RowLimit></View>"
 
#Batch process list items - to mitigate list threshold issue on larger lists
Do {  
    #Get items from the list in batches
    $ListItems = $List.GetItems($Query)
    $Context.Load($ListItems)
    $Context.ExecuteQuery()
           
    $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
  
    #Loop through each List item
    ForEach($ListItem in $ListItems)
    {
        $ListItem.Retrieve("HasUniqueRoleAssignments")
        $Context.ExecuteQuery()

        If($ListItem.HasUniqueRoleAssignments -eq $true)
        {
            #Get List Item Permissions
            $Context.Load($ListItem.RoleAssignments)
            $Context.ExecuteQuery()

            Foreach($RoleAssignment in $ListItem.RoleAssignments)
            {
                $Context.Load($RoleAssignment.Member)
                $Context.executeQuery()
                #Is the current user is the user we search for?
                If($RoleAssignment.Member.LoginName -eq $User.LoginName)
                {
                    #Remove User from List Item Permissions
                    $ListItem.RoleAssignments.GetByPrincipal($User).DeleteObject()
                    $Context.ExecuteQuery()

                    Write-host -ForegroundColor Green ("User Removed from List Item Permissions ID {0} at {1}" -f $ListItem.ID,$ListItem["FileRef"])
                }
            }
        }
    }
    $Context.ExecuteQuery()
} While ($Query.ListItemCollectionPosition -ne $null) 

Tập lệnh PowerShell này xóa người dùng khỏi tất cả các mục mà người dùng có quyền truy cập.

PnP PowerShell để xóa người dùng khỏi tất cả các tệp và thư mục trong Thư viện tài liệu

Lần này, hãy xóa người dùng khỏi tất cả các tệp và thư mục ở bất kỳ nơi nào anh ta có quyền với sự trợ giúp của PnP PowerShell.


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/Marketing"
$ListName="Branding"
$UserAccount = "i:0#.f|membership|steve@Crescent.com"

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
    
    #Get the User
    $User = Get-PnPUser -Identity $UserAccount -ErrorAction Stop

    #Get all list items
    $ListItems =  Get-PnPListItem -List $ListName -PageSize 500 -Fields ID
    $ItemCount = $ListItems.Count

    #Iterate through each list item
    $Counter=1
    ForEach($ListItem in $ListItems)
    {
        #Display a progress bar
        Write-Progress -PercentComplete ($Counter / $ItemCount * 100) -Activity "Processing Items from List:" -Status "Checking Item '$($ListItem.FieldValues.FileRef)' ($Counter of $ItemCount)"

        #Check if the Item has unique permissions
        $HasUniquePermissions = Get-PnPProperty -ClientObject $ListItem -Property "HasUniqueRoleAssignments"
        If($HasUniquePermissions)
        { 
            #Get Permissions Assigned to the Item
            $RoleAssignments = Get-PnPProperty -ClientObject $ListItem -Property RoleAssignments
 
            #Remove user from Item permissions - If Found!
            [Bool]$UserFound = $false
            ForEach($RoleAssignment in $RoleAssignments)
            {
                $Member =  Get-PnPProperty -ClientObject $RoleAssignment -Property Member
                If($Member.LoginName -eq $User.LoginName)
                {
                    $UserFound = $True
                    $ListItem.RoleAssignments.GetByPrincipal($User).DeleteObject()
                    Invoke-PnPQuery
                }
            }
            If($UserFound) { Write-host -f Green "Removed user from $($Listitem.FileSystemObjectType) at '$($ListItem.FieldValues.FileRef)' Permissions!" }
        }
        $Counter++
    }
}
Catch {
    write-host -f Red "Error Removing user from List Items:" $_.Exception.Message
}

Tập lệnh này kết nối với trang SharePoint Online. Sau đó, nó truy xuất danh sách hoặc thư viện tài liệu đã chỉ định và tất cả các mục trong đó. Đối với mỗi mục có quyền riêng biệt, tập lệnh sẽ kiểm tra xem người dùng có quyền hay không. Nếu có, tập lệnh sẽ xóa quyền của người dùng khỏi mục đó. Bằng cách sử dụng tập lệnh PowerShell từ hướng dẫn này, bạn có thể xóa quyền của người dùng khỏi tất cả tài liệu trong thư viện tài liệu SharePoint Online. Đây có thể là một công cụ hữu ích để thu hồi quyền truy cập của người dùng không còn yêu cầu quyền đó nữa hoặc khi bạn muốn thực hiện thay đổi đối với quyền cho nhiều tài liệu cùng một lúc.

Bài viết liên quan