【问题标题】:Fetch user roles and permissions for SharePoint Online using PnP使用 PnP 获取 SharePoint Online 的用户角色和权限
【发布时间】:2020-04-08 16:18:34
【问题描述】:

我希望使用 SharePoint-PnP 为网站集获取 SharePoint 组角色和权限。

我能够使用 $Web.SiteGroups 检索 SharePoint 组,但找不到用于获取角色和权限的属性。

使用以下代码 sn-p 检索组 ID、标题和描述。

#Import the required DLL
Import-Module 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll'
Import-Module 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
#OR
#Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll'
#Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'

#Mysite URL
$site = 'https://test.test.com/sites/sitename'

#Admin User Principal Name
$admin = 'LoginID'

#Get Password as secure String
#$password = Read-Host 'Enter Password' -AsSecureString
$password = Read-Host -Prompt "Enter password" -AsSecureString 


#Get the Client Context and Bind the Site Collection
$context = New-Object Microsoft.SharePoint.Client.ClientContext($site)

#Authenticate
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($admin , $password)
$context.Credentials = $credentials

$list = $context.Web.Lists.GetByTitle('ListName')

$web = $context.Web
$context.Load($web)
$context.Load($web.SiteGroups)
$context.Load($list)
$context.ExecuteQuery()

foreach($x in $web.SiteGroups)
{
    Write-Host $x.Id
    Write-Host $x.Title
    Write-Host $x.Description
}
$list.Update()

我没有使用 SharePoint Online DLL 的选项,因为我无权以租户管理员身份运行脚本,但以网站集管理员身份运行。

如果这完全可以使用 PnP 来实现会很有帮助吗?欢迎任何其他解决方案。

【问题讨论】:

    标签: powershell sharepoint sharepoint-online


    【解决方案1】:

    试试这个 pnp 脚本来获取站点中的组角色和权限:

    $cred = get-credential
    Connect-PnPOnline -Url "https://tenant.sharepoint.com/sites/dev" -Credentials $cred
    $web = Get-PnPWeb -Includes RoleAssignments
    foreach($ra in $web.RoleAssignments) {
        $member = $ra.Member
        $loginName = get-pnpproperty -ClientObject $member -Property LoginName
        $rolebindings = get-pnpproperty -ClientObject $ra -Property RoleDefinitionBindings
        write-host "$($loginName) - $($rolebindings.Name)"
        write-host  
    }
    

    【讨论】:

    • 答案对我有用。从来不知道这可以这么容易地实现。感谢您为我节省了数百万小时。
    • 嘿@Jerry_MSFT,只是一个简单的问题,我如何才能同时检索组描述?因为我在 RoleAssignments 中找不到任何可以满足我要求的属性。
    猜你喜欢
    • 1970-01-01
    • 2020-05-25
    • 2011-05-04
    • 1970-01-01
    • 2021-05-29
    • 1970-01-01
    • 2020-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多