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

[玩转系统] SharePoint Online:使用 PowerShell 获取所有权限级别

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

SharePoint Online:使用 PowerShell 获取所有权限级别


要求:使用 PowerShell 获取 SharePoint Online 网站中的权限级别。

如何获取SharePoint Online网站的所有权限级别?

在 SharePoint Online 中,您可以向用户和组分配不同的权限级别,以控制他们在网站上可以执行和不能执行的操作。例如,您可能希望您的营销团队能够完全访问网站上的所有内容,同时授予其他用户读取权限。此博文将向您展示如何获取 SharePoint Online 中网站的所有权限级别。

要查看 SharePoint Online 网站中的所有权限级别,请执行以下操作:

  1. 导航到您的 SharePoint Online 网站 >> 单击设置齿轮 >> 选择网站设置。 (站点权限>>现代站点中的高级权限设置)。
  2. 单击站点设置页面上的站点权限链接 >> 单击功能区中的权限级别

    [玩转系统] SharePoint Online:使用 PowerShell 获取所有权限级别

  3. “权限级别”页面列出了站点中可用的所有权限级别。

    [玩转系统] SharePoint Online:使用 PowerShell 获取所有权限级别

如果您想查看当前分配给环境中的用户或组的权限,这非常有用。

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"
 
Function Get-SPOPermissionLevels()
{
  param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL        
    )
    Try { 
        #Get 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 permission levels
        $RoleDefColl=$Ctx.web.RoleDefinitions
        $Ctx.Load($RoleDefColl)
        $Ctx.ExecuteQuery()
    
        #Loop through all role definitions
        ForEach($RoleDef in $RoleDefColl)
        {
            Write-Host -ForegroundColor Green $RoleDef.Name
        }
     }
    Catch {
        write-host -f Red "Error getting permission Levels!" $_.Exception.Message
    }
}
 
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/Ops/"

#Call the function 
Get-SPOPermissionLevels -SiteURL $SiteURL 

此脚本获取在 SharePoint Online 网站集中配置的所有权限级别。

SharePoint Online PowerShell 获取权限级别

如果您想在 PowerShell 脚本中获得特定的权限级别,可以使用以下命令:


#Get the permission level
$PermissionLevelName ="Read"
$PermissionLevel = $web.RoleDefinitions.GetByName($PermissionLevelName)
$Ctx.Load($PermissionLevel)
$Ctx.ExecuteQuery()

用于获取 SharePoint Online 中的权限级别的 PnP PowerShell

若要获取 SharePoint Online 网站的权限级别,请使用 cmdlet Get-PnPRoleDefinition


#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get Permission levels
Get-PnPRoleDefinition

同样,要获取特定的权限级别,请使用以下命令:


#Get a Permission level
Get-PnPRoleDefinition -Identity "Read"

要获取所有权限级别的 ID,请使用以下命令:


#sharepoint online get permission level id
Get-PnPRoleDefinition | Select Name, ID, Hidden, Description

使用 PnP PowerShell 从租户中的所有站点导出权限级别

如何审核在 SharePoint Online 租户的所有网站上创建的权限级别?以下 PowerShell 脚本循环访问每个网站集并将权限级别导出到 CSV 报告。确保您的帐户对所有站点都具有管理员权限。否则,您将收到“(403) Forbidden”错误。


#Config Variables
$TenantAdminURL = "https://crescent-admin.sharepoint.com"
$CSVOutputPath = "C:\Temp\PermissionLevels.csv"

#Get Credentials to connect
$Cred = Get-Credential

#Connect to Admin Center using PnP Online
Connect-PnPOnline -Url $TenantAdminURL -Credential $Cred

#Get All Site collections - Exclude: Seach Center, Redirect site, Mysite Host, App Catalog, Content Type Hub, eDiscovery and Bot Sites
$SiteCollections = Get-PnPTenantSite | Where {$_.Template -NotIn ("SRCHCEN#0", "REDIRECTSITE#0", "SPSMSITEHOST#0", "APPCATALOG#0", "POINTPUBLISHINGHUB#0", "EDISC#0", "STS#-1")}

$PermissionLevels= @()
#Loop through each site collection
ForEach($Site in $SiteCollections)
{
    Try {
        Write-host "Processing Site:"$Site.URL -f Yellow
        #Connect to the site
        Connect-PnPOnline -Url $Site.URL -Credential $Cred

        #Get Permission levels
        $RoleDefs = Get-PnPRoleDefinition | Where {$_.Hidden -eq $false} | Select -ExpandProperty Name
        $PermLevels = $RoleDefs -join ", "

        #Collect data
        $PermissionLevels += [PSCustomObject][ordered]@{
            SiteName         = $Site.Title
            URL              = $Site.URL
            PermissionLevels = $PermLevels
        }
    }
    Catch {
        write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
    }
}
#Export the data to CSV Report
$PermissionLevels | Export-Csv -Path $CSVOutputPath -NoTypeInformation

此 PowerShell 脚本会迭代租户中的所有站点,提取并生成权限级别报告:

[玩转系统] SharePoint Online:使用 PowerShell 获取所有权限级别

  • SharePoint Online:使用 PowerShell 删除权限级别
  • 如何在 SharePoint Online 中创建权限级别?
  • SharePoint 权限级别 - 解释
  • PowerShell 用于更新 SharePoint Online 中的权限级别

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

取消回复欢迎 发表评论:

关灯