使用PnP获取SharePoint Online的用户角色和权限

时间:2019-12-16 08:07:19

标签: powershell sharepoint sharepoint-online

我正在寻求使用用于网站集的SharePoint-PnP来获取SharePoint组角色和权限。

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

使用以下代码段检索组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完全可以做到这一点,将很有帮助?任何其他解决方案都欢迎。

1 个答案:

答案 0 :(得分:2)

尝试使用此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  
}

enter image description here