SharePoint Online: Xóa tất cả các quyền duy nhất khỏi tuyển tập trang bằng PowerShell
Yêu cầu: Xóa các quyền duy nhất khỏi tuyển tập trang SharePoint Online và khôi phục quyền kế thừa từ cấp độ gốc.
Làm cách nào để đặt lại quyền SharePoint về mặc định?
Nếu bạn đã từng gặp tình huống cần đặt lại quyền SharePoint nhưng không biết cách thực hiện thì đây chính là bài viết dành cho bạn! Trong hướng dẫn nhanh chóng và dễ dàng này, chúng tôi sẽ giải thích tất cả những gì bạn cần biết về việc đặt lại quyền trên site Sharepoint, sử dụng quyền kế thừa của danh sách hoặc thư viện làm ví dụ. Để đặt lại Quyền SharePoint về mặc định, hãy làm theo các bước sau:
- Đăng nhập vào trang SharePoint Online của bạn >> Điều hướng đến danh sách hoặc thư viện.
- Nhấp vào bánh răng “Cài đặt” và chọn “Cài đặt danh sách”.
- Trong trang “Cài đặt danh sách”, hãy nhấp vào “Quyền cho danh sách này” hoặc “Quyền cho thư viện tài liệu này”.
Bạn sẽ thấy “Thư viện này có các quyền duy nhất”. biểu ngữ ở trên cùng. Nhấp vào nút “Xóa quyền duy nhất” và xác nhận lời nhắc đặt lại quyền của thư viện tài liệu về mặc định.
Quy trình tương tự áp dụng để đặt lại các quyền duy nhất của bất kỳ đối tượng SharePoint nào chẳng hạn như trang con, thư mục, tệp, v.v.
Tập lệnh PowerShell để khôi phục kế thừa quyền trong SharePoint Online
SharePoint Online cho phép chúng tôi quản lý quyền ở mức chi tiết hơn khi chúng tôi cần các quyền duy nhất đối với các đối tượng như site con, danh sách, thư mục hoặc mục danh sách. Theo mặc định, bất kỳ đối tượng nào chúng tôi tạo trong trang web đều kế thừa quyền từ đối tượng mẹ của nó. Ví dụ: Một danh sách được tạo trong site con sẽ kế thừa quyền từ site đó.
Tuy nhiên, bạn nên giữ nguyên quyền kế thừa quyền, vì quyền kế thừa bị hỏng ở cấp mục danh sách trang con sẽ tạo thêm gánh nặng cho Quản trị viên và hiệu suất của trang web. Khi bạn có hàng trăm mục với các quyền riêng biệt, việc giải quyết các vấn đề về quyền sẽ trở nên phức tạp hơn. PowerShell này loại bỏ các quyền duy nhất trên nội dung của tuyển tập trang nhất định, chẳng hạn như:
- Tất cả các trang con
- Danh sách và thư viện
- Thư mục và các mục danh sách.
#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 Delete Unique Permission from a Web and its content
Function Reset-SPOUniquePermission([Microsoft.SharePoint.Client.Web]$Web)
{
Write-host -f Magenta "`nSearching Unique Permissions on the Site:"$web.Url
#Check if the given site is using unique permissions
$Web.Retrieve("HasUniqueRoleAssignments")
$Ctx.ExecuteQuery()
#Get the Root Web
$RootWeb = $ctx.site.RootWeb
$Ctx.Load($RootWeb)
$Ctx.ExecuteQuery()
### Reset broken inheritance on the Web
If($Web.HasUniqueRoleAssignments -and $Web.ID -ne $RootWeb.ID)
{
#powershell to delete unique permissions of a subsite in sharepoint online
$Web.ResetRoleInheritance()
$Web.Update()
$Ctx.ExecuteQuery()
Write-host -f Green "`t Unique Permissions Removed from the Site: $SiteURL!"
}
### Reset unique permission in Lists
Write-host -f Magenta "`t Searching Unique Permissions on the Lists"
$Lists = $Web.Lists
$Ctx.Load($Lists)
$Ctx.ExecuteQuery()
#Exclude system lists
$ExcludedLists = @("App Packages","appdata","appfiles","Apps in Testing","Cache Profiles","Composed Looks","Content and Structure Reports","Content type publishing error log","Converted Forms",
"Device Channels","Form Templates","fpdatasources","Get started with Apps for Office and SharePoint","List Template Gallery", "Long Running Operation Status","Maintenance Log Library", "Style Library",
,"Master Docs","Master Page Gallery","MicroFeed","NintexFormXml","Quick Deploy Items","Relationships List","Reusable Content","Search Config List", "Solution Gallery", "Site Collection Images",
"Suggested Content Browser Locations","TaxonomyHiddenList","User Information List","Web Part Gallery","wfpub","wfsvc","Workflow History","Workflow Tasks", "Preservation Hold Library")
#Iterate through each list
ForEach($List in $Lists)
{
$Ctx.Load($List)
$Ctx.ExecuteQuery()
If($ExcludedLists -NotContains $List.Title -and $List.Hidden -eq $false)
{
#Check if the given site is using unique permissions
$List.Retrieve("HasUniqueRoleAssignments")
$Ctx.ExecuteQuery()
#Reset broken inheritance of the list
If($List.HasUniqueRoleAssignments)
{
#delete unique permissions of a subsite in sharepoint online powershell
$List.ResetRoleInheritance()
$List.Update()
$Ctx.ExecuteQuery()
Write-host -f Green "`t`tUnique Permissions Removed from the List: '$($List.Title)'"
}
Write-host -f Magenta "`t`t Searching Unique Permissions on the Lists Items of '$($List.Title)'"
#Query to batch process
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "<View Scope='RecursiveAll'><RowLimit>2000</RowLimit></View>"
### Reset unique permission on List items
Do {
#Get all items from the list - in batches
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
$Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
#Loop through each List item
ForEach($ListItem in $ListItems)
{
$ListItem.Retrieve("HasUniqueRoleAssignments")
$Ctx.ExecuteQuery()
if ($ListItem.HasUniqueRoleAssignments -eq $true)
{
#Reset Permission Inheritance
$ListItem.ResetRoleInheritance()
Write-host -ForegroundColor Green "`t`t`t Unique Permissions Removed and Inheritence Restored on Item ID:" $ListItem.ID
}
}
$Ctx.ExecuteQuery()
} While ($Query.ListItemCollectionPosition -ne $null)
}
}
#Process each subsite in the site
$Subsites = $Web.Webs
$Ctx.Load($Subsites)
$Ctx.ExecuteQuery()
Foreach ($SubSite in $Subsites)
{
#Call the function Recursively
Reset-SPOUniquePermission($Subsite)
}
}
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
#Get Credentials to connect
$Cred = Get-Credential
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
#Get the Web
$Web = $Ctx.Web
$Ctx.Load($Web)
$Ctx.ExecuteQuery()
#Call the function to delete unique permission from all sites in the site collection
Reset-SPOUniquePermission $Web
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
Xin lưu ý, các danh sách và thư viện cụ thể như “Thư viện kiểu” cần có các quyền duy nhất để cho phép mọi người sử dụng tài nguyên của họ. Vì vậy, Chúng tôi đã loại trừ danh sách hệ thống và thư viện.
Phần kết luận:
Bằng cách làm theo các bước được nêu trong bài viết này, bạn có thể xóa thành công tất cả các quyền duy nhất khỏi tuyển tập trang SharePoint Online bằng PowerShell. Quá trình này có thể đặc biệt hữu ích nếu bạn muốn đặt lại tất cả các quyền duy nhất cho tất cả các đối tượng, chẳng hạn như site con, danh sách và thư viện, thư mục, mục danh sách và tệp trong site SharePoint của bạn. Tuy nhiên, điều quan trọng cần lưu ý là việc loại bỏ tất cả các quyền duy nhất có thể có ý nghĩa quan trọng đối với môi trường SharePoint của bạn vì không thể đảo ngược được! Vì vậy, chúng tôi khuyên bạn nên thực hiện quá trình này một cách thận trọng.

