当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 从网站集中删除所有唯一权限

作者:精品下载站 日期:2024-12-14 21:10:03 浏览:14 分类:玩电脑

SharePoint Online:使用 PowerShell 从网站集中删除所有唯一权限


要求:从 SharePoint Online 网站集删除唯一权限并恢复从父级网站集继承的权限。

[玩转系统] SharePoint Online:使用 PowerShell 从网站集中删除所有唯一权限

如何将 SharePoint 权限重置为默认值?

如果您曾经遇到过需要重置 SharePoint 权限但不知道如何操作的情况,那么这篇文章就是为您准备的!在这个快速简单的指南中,我们将使用列表或库的权限继承作为示例,解释有关在 Sharepoint 站点上重置权限所需了解的所有信息。要将 SharePoint 权限重置为默认值,请执行以下步骤:

  1. 登录到您的 SharePoint Online 网站 >> 导航到列表或库。
  2. 单击“设置”齿轮并选择“列表设置”。
  3. 在“列表设置”页面中,单击“此列表的权限”或“此文档库的权限”。
  4. 您应该看到“此库具有独特的权限”。顶部的横幅。单击“删除唯一权限”按钮并确认提示,将文档库权限重置为默认值。

    [玩转系统] SharePoint Online:使用 PowerShell 从网站集中删除所有唯一权限

相同的过程适用于重置任何 SharePoint 对象(例如子网站、文件夹、文件等)的唯一权限。

用于恢复 SharePoint Online 中的权限继承的 PowerShell 脚本

当我们需要对子网站、列表、文件夹或列表项等对象的唯一权限时,SharePoint Online 允许我们更精细地管理权限。默认情况下,我们在站点中创建的任何对象都会继承其父对象的权限。例如,在子网站中创建的列表会继承该网站的权限。

但是,建议保持权限继承完整,因为子站点列表项级别的继承被破坏会给管理员和站点性能增加额外的负担。当您拥有数百个具有唯一权限的项目时,在解决权限问题的同时会增加更多的复杂性。此 PowerShell 删除对给定网站集内容的唯一权限,例如:

  • 所有子网站
  • 列表和库
  • 文件夹和列表项。

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\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
}

请注意,特定列表和库(例如“样式库”)需要具有独特的权限,以允许每个人使用其资源。因此,我们排除了系统列表和库。

结论:

通过执行本文中概述的步骤,您可以使用 PowerShell 成功从 SharePoint Online 网站集中删除所有唯一权限。如果您想要重置 SharePoint 网站中所有对象(例如子网站、列表和库、文件夹、列表项和文件)的所有唯一权限,此过程可能特别有用。但是,请务必注意,删除所有唯一权限可能会对您的 SharePoint 环境产生重大影响,因为它无法逆转!因此,建议谨慎执行此过程。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯