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

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

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

SharePoint Online:使用 PowerShell 查找具有唯一权限的所有列表


要求:使用 PowerShell 获取具有唯一权限的所有列表和库。

如何检查列表是否使用唯一权限或从父级继承权限?

要获取列表或库是否具有唯一权限,请执行以下步骤:

  1. 导航到列表,然后转到列表或库设置。
  2. 单击列表设置页面上的“此列表/文档库的权限”链接。
  3. 列表设置页面提供有关列表是否具有唯一权限的信息。如果列表或库具有唯一权限,您将收到文本“此列表/库具有唯一权限”;否则,“此列表/库继承其父级的权限。”

    [玩转系统] 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/retail"
$ListName="Documents"

#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 the List
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()

#Check if list has unique permissions
$List.Retrieve("HasUniqueRoleAssignments")
$Ctx.ExecuteQuery()

Write-Host "List has Unique Permissions?": $List.HasUniqueRoleAssignments

在 SharePoint Online 网站中查找具有唯一权限的所有列表和库:

让我们稍微修改一下脚本,以从 SharePoint Online 网站获取所有唯一的权限列表和库。要使用 PowerShell 在 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"

#Define Parameter values
$SiteURL="https://crescent.sharepoint.com"

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
    $ListCollection = $Ctx.Web.Lists
    $Ctx.Load($ListCollection)
    $Ctx.ExecuteQuery()

    #Iterate through each list - Ignore Hidden Lists
    ForEach($List in $ListCollection | Where {$_.Hidden -eq $False})
    {
        #Check if list has unique permissions
        $List.Retrieve("HasUniqueRoleAssignments")
        $Ctx.ExecuteQuery()
        If($List.HasUniqueRoleAssignments -eq $true)
        {
            Write-Host -f Green "List '$($List.Title)' has Unique Permissions"
        }
        else
        {
            Write-Host -f Yellow "List '$($List.Title)' is inhering Permissions from the Parent"
        }
    }
}
Catch {
    write-host -f Red "Error Checking Unique Permissions!" $_.Exception.Message
}

此脚本将连接到您的 SharePoint Online 网站,获取网站中的所有列表和库,然后循环访问每个列表以检查它是否具有唯一权限。如果列表具有唯一权限,则其标题将显示在控制台中。

PnP PowerShell:获取具有唯一权限的所有列表和库

这次,我们从 SharePoint Online 网站集中获取具有唯一权限的所有列表和库。


#Function to Get Lists and Libraries with Unique Permission from a Site collection
Function Get-UniquePermissionLists($SiteURL)
{
    #Connect to SharePoint Online Site from PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive

    #Function to Get Lists with Unique Permissions from the web
    Function Get-PnPUniquePermissionLists([Microsoft.SharePoint.Client.Web]$Web)
    {
        Write-host "Searching Lists and Libraries with Unique Permissions at:"$Web.Url -f Yellow
        Connect-PnPOnline -Url $Web.URL -Interactive
        #Get All Lists from the web
        $Lists = Get-PnPList -Includes HasUniqueRoleAssignments
    
        #Exclude system lists
        $ExcludedLists = @("Content and Structure Reports","Form Templates","Images","Pages","Preservation Hold Library", "Site Pages", "Site Assets",
                             "Site Collection Documents", "Site Collection Images","Style Library","Reusable Content","Workflow History","Workflow Tasks")
              
        #Iterate through lists
        ForEach($List in $Lists)
        {
            #Filter Lists - Exclude System Lists, hiddenlists and get only lists with unique permissions
            If($List.Hidden -eq $False -and $ExcludedLists -notcontains $List.Title -and $List.HasUniqueRoleAssignments)
            {
                Write-host "`tFound a List '$($List.Title)' with Unique Permission at '$($List.RootFolder.ServerRelativeUrl)'" -f Green
            }
        }
    }
    #Call the function for Each Web
    Get-PnPSubWeb -Recurse -IncludeRootWeb | ForEach-Object { Get-PnPUniquePermissionLists($_)}    
 }

#Call the function
Get-UniquePermissionLists "https://Crescent.sharepoint.com/Sites/Marketing"

这些脚本为您提供具有独特权限的所有列表和库。要使用 PowerShell 从 SharePoint Online 删除唯一权限,请使用:SharePoint Online:使用 PowerShell 从列表中删除唯一权限

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

取消回复欢迎 发表评论:

关灯