使用 PnP 获取 SharePoint Online 的用户角色和权限
Fetch user roles and permissions for SharePoint Online using PnP
我希望使用 SharePoint-PnP 为站点 Collection 获取 SharePoint 组角色和权限。
我能够使用 $Web.SiteGroups
检索 SharePoint 组,但未能找到用于获取角色和权限的属性。
使用以下代码片段检索群组 ID、标题和说明。
#Import the required DLL
Import-Module 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll'
Import-Module 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
#OR
#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'
#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 的选项,因为我无权以租户管理员身份访问 运行 脚本,但以站点 collection 管理员身份访问脚本。
如果使用 PnP 可以完全实现这一点,那将会很有帮助吗?欢迎任何其他解决方案。
试试这个 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
}
我希望使用 SharePoint-PnP 为站点 Collection 获取 SharePoint 组角色和权限。
我能够使用 $Web.SiteGroups
检索 SharePoint 组,但未能找到用于获取角色和权限的属性。
使用以下代码片段检索群组 ID、标题和说明。
#Import the required DLL
Import-Module 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll'
Import-Module 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
#OR
#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'
#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 的选项,因为我无权以租户管理员身份访问 运行 脚本,但以站点 collection 管理员身份访问脚本。
如果使用 PnP 可以完全实现这一点,那将会很有帮助吗?欢迎任何其他解决方案。
试试这个 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
}