-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #596 from reshmee011/NewSampleToGetPermissionsfrom…
…SitesAndGroups New sample to get membership details from sites
- Loading branch information
Showing
3 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
--- | ||
plugin: add-to-gallery | ||
--- | ||
|
||
# Get membership report of site(s) within tenant | ||
|
||
## Summary | ||
|
||
The scripts get membership permission report of site(s) within tenant and export it to a CSV file. The report retrieves | ||
- Site Admins | ||
- m365 Group Owners | ||
- m365 Group Members | ||
- m365 Group Guests | ||
- Site Owners | ||
- Site Members | ||
- Site Visitors | ||
|
||
![PnP Powershell result](assets/preview.png) | ||
|
||
# [PnP PowerShell](#tab/pnpps) | ||
```powershell | ||
$AdminCenterURL="https://contoso-admin.sharepoint.com/"# Connect to SharePoint Online admin center | ||
Connect-PnPOnline -Url $AdminCenterURL -Interactive | ||
$dateTime = (Get-Date).toString("dd-MM-yyyy") | ||
$invocation = (Get-Variable MyInvocation).Value | ||
$directorypath = Split-Path $invocation.MyCommand.Path | ||
$fileName = "m365GroupUsersReport-" + $dateTime + ".csv" | ||
$OutPutView = $directorypath + "\Logs\"+ $fileName | ||
# Array to Hold Result - PSObjects | ||
$m365GroupCollection = @() | ||
#Amend query to retrieve the sites within tenant | ||
$m365Sites = Get-PnPTenantSite -Detailed | Where-Object {($_.Url -like '*/Dev-*' -or $_.Url -like '*/Test-*' -or $_.Url -like '*/Uat-*' -or $_.Template -eq 'TEAMCHANNEL#1') -and $_.Template -ne 'RedirectSite#0' } | ||
$m365Sites | ForEach-Object { | ||
$ExportVw = New-Object PSObject | ||
$ExportVw | Add-Member -MemberType NoteProperty -name "Site Name" -value $_.Title | ||
$m365GroupOwnersName=""; | ||
$m365GroupMembersName=""; | ||
$m365GroupGuestsName = ""; | ||
$groupId = $_.GroupId; | ||
$siteUrl = $_.Url; | ||
#Check if site template is a Team template to retrieve the m365 group membership | ||
if($_.Template -eq "GROUP#0") | ||
{ | ||
$m365GroupOwnersName = (Get-PnPMicrosoft365GroupOwner -Identity $groupId -ErrorAction Ignore| select-object -ExpandProperty DisplayName ) -join ";"; | ||
$m365GroupMembersName = (Get-PnPMicrosoft365GroupMember -Identity $groupId -ErrorAction Ignore| select-object -ExpandProperty DisplayName) -join ";"; | ||
$m365GroupGuestsName = (Get-PnPMicrosoft365GroupMember -Identity $groupId -ErrorAction Ignore |Where-Object UserType -eq Guest | select-object -ExpandProperty DisplayName) -join ";"; | ||
} | ||
$ExportVw | Add-Member -MemberType NoteProperty -name "Group Owners" -value $m365GroupOwnersName | ||
$ExportVw | Add-Member -MemberType NoteProperty -name "Group Members" -value $m365GroupMembersName | ||
$ExportVw | Add-Member -MemberType NoteProperty -name "Group Guests" -value $m365GroupGuestsName | ||
Connect-PnPOnline -Url $siteUrl -Interactive | ||
$site = Get-PnPSite -Includes ID | ||
$ExportVw | Add-Member -MemberType NoteProperty -name "Site Id" -value $site.Id | ||
$siteadmins = (Get-PnPSiteCollectionAdmin | select-object -ExpandProperty Title) -join ";"; | ||
$ExportVw | Add-Member -MemberType NoteProperty -name "Site admins" -value $siteadmins | ||
$siteowners = (Get-PnPGroupMember -Group (Get-PnPGroup -AssociatedOwnerGroup) | select-object -ExpandProperty Title) -join ";" | ||
$ExportVw | Add-Member -MemberType NoteProperty -name "Site owners" -value $siteowners | ||
$sitemembers = (Get-PnPGroupMember -Group (Get-PnPGroup -AssociatedMemberGroup) | select-object -ExpandProperty Title) -join ";" | ||
$ExportVw | Add-Member -MemberType NoteProperty -name "Site members" -value $sitemembers | ||
$sitevisitors = (Get-PnPGroupMember -Group (Get-PnPGroup -AssociatedVisitorGroup) | select-object -ExpandProperty Title) -join ";" | ||
$ExportVw | Add-Member -MemberType NoteProperty -name "Site visitors" -value $sitevisitors | ||
$m365GroupCollection += $ExportVw | ||
} | ||
# Export the result array to CSV file | ||
$m365GroupCollection | sort-object "Site Name" |Export-CSV $OutPutView -Force -NoTypeInformation | ||
# Disconnect SharePoint online connection | ||
Disconnect-PnPOnline | ||
``` | ||
[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)] | ||
|
||
*** | ||
|
||
## Contributors | ||
|
||
| Author(s) | | ||
|-----------| | ||
| [Reshmee Auckloo](https://github.com/reshmee011)| | ||
|
||
[!INCLUDE [DISCLAIMER](../../docfx/includes/DISCLAIMER.md)] | ||
<img src="https://m365-visitor-stats.azurewebsites.net/script-samples/scripts/spo-get-sites-membership-report" aria-hidden="true" /> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 61 additions & 0 deletions
61
scripts/spo-get-sites-membership-report/assets/sample.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
[ | ||
{ | ||
"name": "spo-get-sites-membership-report", | ||
"source": "pnp", | ||
"title": "Get membership report of a site", | ||
"shortDescription": "The script export site membership details to a CSV file", | ||
"url": "https://pnp.github.io/script-samples/spo-get-sites-membership-report/README.html", | ||
"longDescription": [ | ||
"The script gets site membership details including Site Admins, m365 Group Owners, m365 Group Members, m365 Group Guests, Site Owners, Site Members and Site Visitors and export it to a CSV file." | ||
], | ||
"creationDateTime": "2023-10-12", | ||
"updateDateTime": "2023-10-12", | ||
"products": [ | ||
"SharePoint" | ||
], | ||
"metadata": [ | ||
{ | ||
"key": "PNP-POWERSHELL", | ||
"value": "2.2.0" | ||
}, | ||
{ | ||
"key": "POWERSHELL", | ||
"value": "7.3.6" | ||
} | ||
], | ||
"categories": [ | ||
"Data", | ||
"Report", | ||
"Security" | ||
], | ||
"tags": [ | ||
"Get-PnPTenantSite", | ||
"Get-PnPMicrosoft365GroupOwner", | ||
"Get-PnPMicrosoft365GroupMember", | ||
"Get-PnPSiteCollectionAdmin", | ||
"Get-PnPGroupMember" | ||
], | ||
"thumbnails": [ | ||
{ | ||
"type": "image", | ||
"order": 100, | ||
"url": "https://raw.githubusercontent.com/pnp/script-samples/main/scripts/spo-get-sites-membership-report/assets/preview.png", | ||
"alt": "" | ||
} | ||
], | ||
"authors": [ | ||
{ | ||
"gitHubAccount": "reshmee011", | ||
"pictureUrl": "https://avatars.githubusercontent.com/u/7693852?v=4", | ||
"name": "Reshmee Auckloo" | ||
} | ||
], | ||
"references": [ | ||
{ | ||
"name": "Want to learn more about PnP PowerShell and the cmdlets", | ||
"description": "Check out the PnP PowerShell site to get started and for the reference to the cmdlets.", | ||
"url": "https://aka.ms/pnp/powershell" | ||
} | ||
] | ||
} | ||
] |