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

[玩转系统] SharePoint Online:使用 PowerShell 获取具有唯一权限的所有列表项

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

SharePoint Online:使用 PowerShell 获取具有唯一权限的所有列表项


要求:使用 PowerShell 获取具有唯一权限的所有 SharePoint Online 列表项目。

如何检查列表项是否具有唯一权限或从父级继承权限?

若要了解文档库中的 SharePoint Online 列表项目或文件是否具有唯一权限,请按照下列步骤操作:

  1. 导航到列表/库,然后选择列表项。
  2. 在详细信息窗格中,单击“管理访问”链接(在经典体验中,单击“高级”>>“共享对象”),然后单击“高级”链接。
  3. 这将带您进入列表项的“高级权限”页面,其中提供有关列表项是否具有唯一权限或从父级继承权限的信息。例如。您将收到文本“此列表项具有独特的权限

    [玩转系统] SharePoint Online:使用 PowerShell 获取具有唯一权限的所有列表项

SharePoint Online:PowerShell 获取具有唯一权限的所有列表项:

让我们使用 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" 

#Define Parameter values
$SiteURL="https://crescent.sharepoint.com/sites/PMO"
$ListName="Projects"
 
Try {
    #Setup Credentials to connect
    $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
         
    #Get All Lists of the web
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()
    Write-host "Total List Items Found:"$List.ItemCount

    #Query to Get 2000 items from the list
    $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 {  
        $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)
            {        
                Write-Host -f Green "List Item '$($ListItem["Title"])' with ID '$($ListItem.ID)' has Unique Permissions"
            }
            else
            {
                Write-Host -f Yellow "List Item '$($ListItem["Title"])' with ID '$($ListItem.ID)' is inhering Permissions from the Parent"
            }
        }
    } While ($Query.ListItemCollectionPosition -ne $null)
 
}
Catch {
    write-host -f Red "Error Checking Unique Permissions!" $_.Exception.Message
}

使用 PnP PowerShell 在 SharePoint Online 列表中查找唯一权限

我们还可以使用 PnP PowerShell 在 SharePoint Online 列表或库中搜索独特的权限。


#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/PMO"
$ListName = "Projects"
  
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get all list items in batches
$ListItems = Get-PnPListItem -List $ListName -PageSize 2000
 
#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)
    {
        Write-Host -f Green "List Item '$($ListItem["Title"])' with ID '$($ListItem.ID)' has Unique Permissions"
    }
    Else
    {
        Write-Host -f Yellow "List Item '$($ListItem["Title"])' with ID '$($ListItem.ID)' is inhering Permissions from its Parent"
    }    
}

按照本文中的脚本,您可以轻松地从 SharePoint 列表中检索项目并对其进行筛选,以仅包含分配了唯一权限的项目。若要从 SharePoint Online 中的所有列表项删除唯一权限,请使用:SharePoint Online:使用 PowerShell 删除列表中所有项目的唯一权限

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

取消回复欢迎 发表评论:

关灯