Get All groups from a SharePoint Site (including Domain Groups) #3105
-
Hello everyone, I am attempting to get all of the groups that have been assigned to a SharePoint site with the following code snippet. As a side effect, this is only pickup up the SharePoint Groups, and not the Domain Groups. I have attempted to use both PnPGroup and the PnPSiteGroup. foreach ($site in $AllSites) {
$PermissionCollection = @()
# Connect to SharePoint Online
Write-Host $site.SiteUrl -ForegroundColor "Green"
Connect-PnPOnline -Url $site.SiteUrl -UseWebLogin
$x = Get-PnpSiteGroup -Site $site.SiteUrl
$z = Get-PnPGroup | Select-Object Title, Users
foreach ($y in $x) {
$group = Get-PnpSiteGroup -Site $site.SiteUrl -Group $y.Title | Select-Object -ExpandProperty Users
# $group = Get-PnPGroup -Group $y.Title | Select-Object -ExpandProperty Users
if ($group.Count -eq 0) {
}
else {
try {
$permission = Get-PnPGroupPermissions -Identity $y.Title
Write-Host $y.Title -ForegroundColor "Yellow"
Write-Host $group -ForegroundColor "Cyan"
$Permissions = New-Object PSObject
$Permissions | Add-Member NoteProperty URL($site.SiteUrl)
$Permissions | Add-Member NoteProperty Title($y.Title)
$Permissions | Add-Member NoteProperty GroupName($y.OwnerTitle)
$permissionString = $permission.Name -join "; "
$Permissions | Add-Member NoteProperty Permission($permissionString)
$groupString = $group -join "; "
$Permissions | Add-Member NoteProperty Users($groupString)
}
catch {
Write-Host $y.Title "Doesn't have any permissions" -ForegroundColor "Red"
}
}
$PermissionCollection += $Permissions
} When I run the above snippet, I will only get the Inventory Members, Owners, and Visitors, but I will not get the SPO Inventory or SPO SharePoint groups. Am I missing a Cmdlet? or is there a way to get it to return the Domain Groups as well? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
The security groups are fetched using Get-PnPUser and are of PrincipalType: SecurityGroup Also you need to load the properties on the web. Something along these lines $web = get-pnpweb -Connection $c -Includes RoleAssignments,SiteGroups, RoleDefinitions,HasUniqueRoleAssignments Hope this helps |
Beta Was this translation helpful? Give feedback.
-
Hello Reading is Ok. Best regards |
Beta Was this translation helpful? Give feedback.
The security groups are fetched using Get-PnPUser and are of PrincipalType: SecurityGroup
Also you need to load the properties on the web. Something along these lines
$web = get-pnpweb -Connection $c -Includes RoleAssignments,SiteGroups, RoleDefinitions,HasUniqueRoleAssignments
$web.RoleAssignments | % { Get-PnPProperty -Property Member -ClientObject $_ -Connection $c| select -expand loginname }
Hope this helps