SharePoint Online: Xóa quyền duy nhất cho tất cả các mục trong danh sách bằng PowerShell
Yêu cầu: Đặt lại tất cả các quyền tùy chỉnh trong tất cả tài liệu của thư viện tài liệu SharePoint Online.
Theo mặc định, Quyền được kế thừa từ cha mẹ ở tất cả các cấp độ của SharePoint. Ví dụ: site con kế thừa quyền từ tuyển tập site mẹ (hoặc site mẹ), danh sách và thư viện kế thừa quyền từ site và các mục trong danh sách kế thừa quyền từ danh sách. Vì vậy, nếu bạn thực hiện bất kỳ thay đổi nào về quyền ở cấp độ gốc, mọi quyền con bên dưới sẽ tự động kế thừa những thay đổi về quyền mà bạn đã thực hiện ở cấp độ gốc trừ khi trẻ đó đang sử dụng các quyền riêng của mình.
Đôi khi, bạn có thể phải thiết lập các quyền duy nhất ở cấp độ chi tiết và tất nhiên, bạn cũng có thể phải đặt lại các quyền bị hỏng.
Làm cách nào để xóa các quyền duy nhất trong SharePoint?
Để đặt lại quyền tùy chỉnh trên các mục hoặc tài liệu trong danh sách SharePoint, hãy làm theo các bước sau:
- Dẫn hướng đến thư viện SharePoint nơi tài liệu của bạn được lưu trữ.
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.
Nhấp vào liên kết “Nâng cao” ở cuối ngăn “Quản lý quyền truy cập” >> và nhấp vào nút “Xóa quyền duy nhất” từ dải băng. Xác nhận lời nhắc một lần!
Được rồi, bây giờ các quyền được đặt thành Kế thừa từ thư viện mẹ của tài liệu. Nhưng chờ đã, việc chọn từng tài liệu riêng lẻ và lặp lại các bước này để xóa các quyền duy nhất thật tẻ nhạt, bạn có đồng ý không? Vì vậy, tôi đã viết tập lệnh PowerShell này để Đặt lại tính kế thừa bị hỏng trên tất cả các mục trong Danh sách SharePoint.
Quyền truy cập hạn chế có nghĩa là người dùng có quyền truy cập vào một đối tượng cơ bản. Ví dụ. Nếu bạn cung cấp quyền duy nhất cho một tài liệu thì ở cấp thư viện tài liệu, người dùng cụ thể sẽ có “Quyền truy cập hạn chế”. Nó cung cấp cho người dùng quyền hạn chế để truy cập vào mục mà anh ta có quyền truy cập. Nếu muốn xóa quyền truy cập hạn chế, bạn cần xóa quyền duy nhất đối với các mục mà người dùng có quyền truy cập.
PowerShell để xóa các quyền duy nhất khỏi Mục danh sách SharePoint Online:
Bạn cần cài đặt SharePoint Online Client SDK trên máy khách của mình để sử dụng mã này: https://www.microsoft.com/en-us/download/details.aspx?id=42038. Đây là cách xóa tất cả các quyền duy nhất trong mục danh sách SharePoint Online
#Load SharePoint CSOM 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"
#Function to remove unique permissions and inherit from the parent
Function Remove-ListItemUniquePermissions
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $ListName,
[Parameter(Mandatory=$true)] [string] $ItemID
)
Try {
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Remove unique permissions and reset inheritance
$List=$Ctx.Web.Lists.GetByTitle($ListName)
$ListItem=$List.GetItemByID($ItemID)
$ListItem.ResetRoleInheritance()
$Ctx.ExecuteQuery()
Write-Host "Unique Permissions are removed and inherited from the Parent!" -ForegroundColor Green
}
Catch {
write-host -f Red "Error Deleting Unique Permissions!" $_.Exception.Message
}
}
#Parameters
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$ItemID="25"
#Call the function to remove unique permissions from a list
Remove-ListItemUniquePermissions -SiteURL $SiteURL -ListName $ListName -ItemID $ItemID
Tập lệnh PowerShell để xóa các quyền duy nhất cho tất cả các mục danh sách trong SharePoint Online
Đây là SharePoint Online PowerShell để đặt lại các quyền duy nhất:
#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/Sales/"
$ListName= "Documents"
$UserName= "Salaudeen@crescent.com"
$Password ="Password goes here"
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Context.Credentials = $credentials
#Get the List
$List = $Context.web.Lists.GetByTitle($ListName)
#Get All List Items
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$ListItems = $List.GetItems($Query)
$Context.Load($ListItems)
$Context.ExecuteQuery()
Write-host "Total Items Found:"$ListItems.Count
#Iterate through each list item
$ListItems | foreach {
#Delete Unique Permission
$_.ResetRoleInheritance()
}
$Context.ExecuteQuery()
Write-host "Broken Permissions are Deleted on All Items!" -ForegroundColor Green
Chúng tôi có thể thực hiện hai cải tiến cho tập lệnh trên:
- Tập lệnh trên không xử lý danh sách lớn có > 5000 mục.
- Đoạn script trên chỉ đơn giản là đặt lại tính kế thừa mà không kiểm tra xem mục danh sách có quyền duy nhất hay không.
Vì vậy, hãy cùng chúng tôi giải quyết các vấn đề trên và đây là tập lệnh được cập nhật:
Xóa quyền duy nhất trong SharePoint Online bằng PowerShell
Đây là PowerShell để đặt lại các quyền duy nhất trong danh sách hoặc thư viện tài liệu lớn (với hơn 5000 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"
$ListName= "Documents"
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Context.Credentials = $Credentials
#Get the List
$List = $Context.web.Lists.GetByTitle($ListName)
$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)
{
#Reset Permission Inheritance
$ListItem.ResetRoleInheritance()
Write-host -ForegroundColor Yellow "Inheritence Restored on Item:" $ListItem.ID
}
}
$Context.ExecuteQuery()
} While ($Query.ListItemCollectionPosition -ne $null)
Write-host "Broken Permissions are Deleted on All Items!" -ForegroundColor Green
PnP PowerShell để xóa các quyền duy nhất khỏi tất cả các mục trong danh sách
Để đặt lại quyền trên một mục danh sách, chúng ta có thể sử dụng lệnh ghép ngắn Set-PnPListItemPermission với nút chuyển “InheritPermissions”.
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$ListName = "Documents"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#Get all list items in batches
$ListItems = Get-PnPListItem -List $ListName -PageSize 500
#Iterate through each list item
ForEach($ListItem in $ListItems)
{
#Check if the Item has unique permissions
$HasUniquePermissions = Get-PnPProperty -ClientObject $ListItem -Property "HasUniqueRoleAssignments"
If($HasUniquePermissions)
{
$Msg = "Deleting Unique Permissions on {0} '{1}' at {2} " -f $ListItem.FileSystemObjectType,$ListItem.FieldValues["FileLeafRef"],$ListItem.FieldValues["FileRef"]
Write-host $Msg
#Delete unique permissions on the list item
Set-PnPListItemPermission -List $ListName -Identity $ListItem.ID -InheritPermissions
}
}
Đặt lại quyền duy nhất cho tất cả các tệp trong thư mục bằng PnP PowerShell
Lần này, hãy sử dụng PnP PowerShell để đặt lại các quyền duy nhất trên tất cả các tệp được lưu trữ trong Thư mục SharePoint Online:
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$FolderServerRelativeURL = "/sites/Marketing/Branding/2018"
#Connect to the site
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the Folder from given URL
$Folder = Get-PnPFolder -Url $FolderServerRelativeURL -Includes ListItemAllFields.ParentList
$ParentList = $Folder.ListItemAllFields.ParentList.Title
#Get All Files from the Folder
$Files = Get-PnPListItem -List $ParentList -FolderServerRelativeUrl $Folder.ServerRelativeUrl | Where {$_.FileSystemObjectType -eq "File"}
#Traverse through each file in the folder
ForEach ($File in $Files)
{
#Check If File has Unique Permissions
$HasUniquePermissions = Get-PnPProperty -ClientObject $File -Property HasUniqueRoleAssignments
If($HasUniquePermissions)
{
#Reset Broken Inheritance
$File.ResetRoleInheritance()
$File.update()
Invoke-PnPQuery
Write-Host "Reset Unique Permissions on File $($File.FieldValues["FileRef"])" -ForegroundColor Green
}
}
Tương tự, bạn có thể đặt lại các quyền duy nhất trong các trang, danh sách và thư mục SharePoint Online bằng PowerShell dưới dạng:
- SharePoint Online: Xóa Quyền duy nhất khỏi Danh sách hoặc Thư viện Tài liệu bằng PowerShell
- SharePoint Online: Xóa các quyền duy nhất khỏi tất cả các thư mục trong Thư viện tài liệu bằng PowerShell
- SharePoint Online: Đặt lại tất cả các quyền duy nhất từ tuyển tập trang bằng PowerShell

