次の例を検討することもできグループの一覧取得の場合:特定のウェブのためのユーザのリストの場合は
# Retrieve web groups
$groups = $context.Web.RoleAssignments.Groups
$context.Load($groups)
$context.ExecuteQuery()
をRoleAssignmentCollection
クラスがUsers
プロパティを公開していないので、それはそう簡単ではありません。
Function Get-WebUsers() {
param(
[Microsoft.SharePoint.Client.Web]$Web = $(throw "Please provide a Web")
)
$ctx = $Object.Context
$assignments = $context.Web.RoleAssignments
$context.Load($assignments)
$context.ExecuteQuery()
$members = @()
$assignments.GetEnumerator() | % {
$member = $context.Web.RoleAssignments.GetByPrincipalId($_.PrincipalId).Member
$context.Load($member)
$members += $member
}
$context.ExecuteQuery()
$users = @()
$members | % {
if($_.PrincipalType -eq [Microsoft.SharePoint.Client.Utilities.PrincipalType]::User) {
$users += $_
}
}
$users
}
例:
Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Function Get-Context([String]$WebUrl,$UserName,$Password) {
$context = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl)
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
return $context
}
Function Get-WebUsers() {
param(
[Microsoft.SharePoint.Client.Web]$Web = $(throw "Please provide a Web")
)
$ctx = $Object.Context
$assignments = $context.Web.RoleAssignments
$context.Load($assignments)
$context.ExecuteQuery()
$members = @()
$assignments.GetEnumerator() | % {
$member = $context.Web.RoleAssignments.GetByPrincipalId($_.PrincipalId).Member
$context.Load($member)
$members += $member
}
$context.ExecuteQuery()
$users = @()
$members | % {
if($_.PrincipalType -eq [Microsoft.SharePoint.Client.Utilities.PrincipalType]::User) {
$users += $_
}
}
$users
}
$Url = "https://contoso.sharepoint.com/news"
$Username = "[email protected]"
$Password = ""
$context = Get-Context -WebUrl $Url -UserName $Username -Password $Password
# Retrieve web groups
$groups = $context.Web.RoleAssignments.Groups
$context.Load($groups)
$context.ExecuteQuery()
write "Group names:"
$groups.GetEnumerator() | % {
$_.Title
}
#Retieve web users
$users = Get-WebUsers -Web $context.Web
write "User names:"
$users | % {
$_.Title
}
$context.Dispose()
要旨:WebUsersAndGroups.ps1
私は実際にそれを使用して作業しました: とにかく次の例では、ウェブサイトのためにユーザーのリストを取得する方法を示しここに投稿するには長すぎるPSスクリプト......... – ArKersten