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

[玩转系统] SharePoint Online:使用 PowerShell 删除列表中所有项目的唯一权限

作者:精品下载站 日期:2024-12-14 14:11:37 浏览:15 分类:玩电脑

SharePoint Online:使用 PowerShell 删除列表中所有项目的唯一权限


要求:重置 SharePoint Online 文档库的所有文档中的所有自定义权限。

默认情况下,在 SharePoint 的所有级别中,权限都是从其父级继承的。例如,子网站从其父网站集(或父网站)继承权限,列表和库从网站继承权限,列表中的项目从列表继承权限。因此,如果您在父级级别对权限进行任何更改,则下面的任何子级都会自动继承您在父级中所做的权限更改,除非子级使用自己的唯一权限。

有时,您可能必须在粒度级别设置唯一的权限,当然,您可能还必须重置损坏的权限。

如何删除 SharePoint 中的唯一权限?

要重置 SharePoint 列表项或文档的自定义权限,请执行以下步骤:

  1. 导航到存储文档的 SharePoint 库。
  2. 右键单击文档并从菜单中选择“管理访问”。

    [玩转系统] SharePoint Online:使用 PowerShell 删除列表中所有项目的唯一权限

  3. 单击“管理访问”窗格底部的“高级”链接>>,然后单击功能区中的“删除唯一权限”按钮。确认提示一次!

    [玩转系统] SharePoint Online:使用 PowerShell 删除列表中所有项目的唯一权限

好的,现在权限已设置为从文档的父库继承。但是等等,选择每个单独的文档并重复这些步骤来删除唯一的权限是很乏味的,您不同意吗?因此,我编写了此 PowerShell 脚本来重置 SharePoint 列表中所有项目的中断继承。

如何摆脱 SharePoint Online 中的访问限制?
有限访问意味着用户有权访问底层对象。例如。如果您在文档库级别为文档提供唯一权限,则特定用户将具有“有限访问权限”。它为用户提供有限的权限来访问他有权访问的项目。如果要删除受限访问权限,则需要删除用户有权访问的项目的唯一权限。

PowerShell 从 SharePoint Online 列表项中删除唯一权限:

您需要在客户端计算机上安装 SharePoint Online 客户端 SDK 才能使用此代码:https://www.microsoft.com/en-us/download/details.aspx?id=42038。以下是如何删除 SharePoint Online 列表项中的所有唯一权限


#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 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 

用于删除 SharePoint Online 中所有列表项的唯一权限的 PowerShell 脚本

以下是用于重置唯一权限的 SharePoint Online PowerShell:


#Load SharePoint Online 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"
  
##Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/Sales/"
$ListName= "Documents"
$UserName= "[email protected]"
$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

我们可以对上面的脚本进行两处改进:

  1. 上面的脚本不能处理包含超过 5000 个项目的大型列表。
  2. 上面的脚本只是重置继承,而不检查列表项是否具有唯一权限。

那么,让我们解决上述问题,这是更新后的脚本:

使用 PowerShell 删除 SharePoint Online 中的独特权限

这是用于重置大型列表或文档库(超过 5000 个项目!)中的唯一权限的 PowerShell


#Load SharePoint Online 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"

##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 删除列表中所有项目的唯一权限

要重置列表项的权限,我们可以使用带有“InheritPermissions”开关的 Set-PnPListItemPermission cmdlet。


#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 
    }
}

使用 PnP PowerShell 重置文件夹中所有文件的唯一权限

这次,我们使用 PnP PowerShell 重置 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
    }
}

同样,您可以使用 PowerShell 重置 SharePoint Online 网站、列表和文件夹中的唯一权限,如下所示:

  • SharePoint Online:使用 PowerShell 从列表或文档库中删除唯一权限
  • SharePoint Online:使用 PowerShell 删除文档库中所有文件夹的唯一权限
  • SharePoint Online:使用 PowerShell 重置网站集的所有唯一权限

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

取消回复欢迎 发表评论:

关灯