Skip to content

Commit

Permalink
Merge pull request #790 from mkm17/spo-site-membership-admins
Browse files Browse the repository at this point in the history
new sample spo get sites membership as admin
  • Loading branch information
pkbullock authored Dec 11, 2024
2 parents cd01edf + 8d20e4e commit a7a47fc
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 0 deletions.
143 changes: 143 additions & 0 deletions scripts/spo-get-sites-membership-as-admin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
---
plugin: add-to-gallery
---

# Get sites membership as an admin

## Summary

Obtaining basic membership information for all SharePoint sites in a tenant can be challenging. Even SharePoint administrators typically cannot access this information unless they are assigned as Site Collection Administrators for each site. This script enables the retrieval of membership details without requiring Site Collection Administrator permissions.

It utilizes special CLI for Microsoft 365 commands to extract this information, without the need for additional background actions.

The `showDataInSeparateRows` parameter determines whether the data is displayed in separate rows or consolidated into a single row per site in output csv.

The `shownProperty` parameter specifies which user property (name, email, or loginName) is present when data is consolidated into one row per site.

![Example Screenshot](assets/preview.png)

### Prerequisites

- The user account that runs the script must have SharePoint Online Administrator access.

# [CLI for Microsoft 365](#tab/cli-m365-ps)

```powershell
function Get-Site-Membership {
param (
[ValidateSet("name", "email", "loginName")]
[string]$shownProperty = "loginName",
[boolean]$showDataInSeparateRows
)
$dateTime = "{0:MM_dd_yy}_{0:HH_mm_ss}" -f (Get-Date)
$csvPath = "site-membership" + $dateTime + ".csv"
#Get Credentials to connect
$m365Status = m365 status
if ($m365Status -match "Logged Out") {
m365 login
}
#Get SharePoint sites
$m365Sites = m365 spo site list -o json | ConvertFrom-Json | Where-Object { $_.Url -like '*/sites/*' -and $_.Template -ne 'RedirectSite#0' }
#filter to include sites with "/sites/" managed path and to exclude the redirect sites
$siteRows = @()
$m365Sites | ForEach-Object {
Write-host "Gets membership information from: " $_.Url
$admins = m365 spo site admin list --siteUrl $_.Url --asAdmin -o json | ConvertFrom-Json
$groups = m365 spo tenant site membership list --siteUrl $_.Url -o json | ConvertFrom-Json
$mappingTable = @{
"email" = "Email"
"loginName" = "LoginName"
"name" = "Title"
}
if ($showDataInSeparateRows) {
$siteUrl = $_.Url
$siteName = $_.Title
$admins | ForEach-Object {
$siteRows = New-Object PSObject -Property ([ordered]@{
SiteName = $siteName
SiteUrl = $siteUrl
Type = $_.IsPrimaryAdmin ? "PrimaryAdmin" :"SecondaryAdmin"
Email = $_.Email
LoginName = $_.LoginName
Title = $_.Title
})
$siteRows | Export-Csv $csvPath -NoTypeInformation -Append
}
$groups.AssociatedOwnerGroup | ForEach-Object {
$siteRows = New-Object PSObject -Property ([ordered]@{
SiteName = $siteName
SiteUrl = $siteUrl
Type = "Owner"
Email = $_.email
LoginName = $_.loginName
Title = $_.name
})
$siteRows | Export-Csv $csvPath -NoTypeInformation -Append
}
$groups.AssociatedMemberGroup | ForEach-Object {
$siteRows = New-Object PSObject -Property ([ordered]@{
SiteName = $siteName
SiteUrl = $siteUrl
Type = "Member"
Email = $_.email
LoginName = $_.loginName
Title = $_.name
})
$siteRows | Export-Csv $csvPath -NoTypeInformation -Append
}
$groups.AssociatedOwnerGroup | ForEach-Object {
$siteRows = New-Object PSObject -Property ([ordered]@{
SiteName = $siteName
SiteUrl = $siteUrl
Type = "Visitor"
Email = $_.email
LoginName = $_.loginName
Title = $_.name
})
$siteRows | Export-Csv $csvPath -NoTypeInformation -Append
}
}
else {
$siteRows = New-Object PSObject -Property ([ordered]@{
SiteName = $_.Title
SiteUrl = $_.Url
Admins = ($admins | ForEach-Object { $_.($mappingTable[$shownProperty]) }) -join ";"
Owners = ($groups.AssociatedOwnerGroup | ForEach-Object { $_.$shownProperty }) -join ";"
Members = ($groups.AssociatedMemberGroup | ForEach-Object { $_.$shownProperty }) -join ";"
Visitors = ($groups.AssociatedVisitorGroup | ForEach-Object { $_.$shownProperty }) -join ";"
})
$siteRows | Export-Csv $csvPath -NoTypeInformation -Append
}
}
# Disconnect SharePoint online connection
m365 logout
}
Get-Site-Membership -shownProperty "name" -showDataInSeparateRows $true
```

[!INCLUDE [More about CLI for Microsoft 365](../../docfx/includes/MORE-CLIM365.md)]

***

## Contributors

| Author(s) |
| ----------------------------------------- |
| [Michał Kornet](https://github.com/mkm17) |


[!INCLUDE [DISCLAIMER](../../docfx/includes/DISCLAIMER.md)]
<img src="https://m365-visitor-stats.azurewebsites.net/script-samples/scripts/spo-get-sites-membership-as-admin" 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.
60 changes: 60 additions & 0 deletions scripts/spo-get-sites-membership-as-admin/assets/sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
[
{
"schema": null,
"name": "spo-get-sites-membership-as-admin",
"version": null,
"source": "pnp",
"title": "Get sites membership as an admin",
"url": "https://pnp.github.io/script-samples/spo-get-sites-membership-as-admin/README.html",
"creationDateTime": "2024-12-06",
"updateDateTime": "2024-12-06",
"shortDescription": "The script retrieves information about a site's default group memberships and administrators for SharePoint admins, without requiring permissions for specific SharePoint sites",
"longDescription": [
"This script shows how to find information about a site's default group memberships (Owners, Members, Visitors) and administrators as SharePoint admin. No special permissions for individual SharePoint sites are required to execute the script. The results will be saved in a CSV file with two export options: data can be displayed in separate rows for each group membership type, or combined into a single row per site."
],
"products": [
"SharePoint"
],
"categories": [
"Data",
"Report"
],
"tags": [
"m365 status",
"m365 login",
"m365 spo site admin list",
"spo tenant site membership list",
"m365 logout"
],
"metadata": [
{
"key": "CLI-FOR-MICROSOFT365",
"value": "10.2.0"
}
],
"thumbnails": [
{
"type": "image",
"order": 100,
"url": "https://raw.githubusercontent.com/pnp/script-samples/main/scripts/spo-get-sites-membership-as-admin/assets/preview.png",
"alt": "preview image for the sample",
"slides": null
}
],
"authors": [
{
"gitHubAccount": "mkm17",
"company": "WM Reply",
"pictureUrl": "https://github.com/mkm17.png",
"name": "Michał Kornet"
}
],
"references": [
{
"name": "Want to learn more about CLI for Microsoft 365 and the commands",
"description": "Check out the CLI for Microsoft 365 site to get started and for the reference to the commands.",
"url": "https://aka.ms/cli-m365"
}
]
}
]

0 comments on commit a7a47fc

Please sign in to comment.