Created
April 18, 2025 12:38
-
-
Save joerodgers/69752454d8f18306009ca66de7b92a2e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#requires -modules "PnP.PowerShell" | |
function Get-TenantInformation | |
{ | |
[cmdletbinding()] | |
param | |
( | |
) | |
$response = Invoke-PnPSPRestMethod -Method "Get" -Url "/_api/TenantInformationCollection" | |
$response.value | Select-Object GeoLocation, TenantAdminDomain | |
} | |
Connect-PnPOnline -Url "https://$env:CDX_TENANT-admin.sharepoint.com" ` | |
-ClientId $env:CDX_CLIENTID ` | |
-Thumbprint $env:CDX_THUMBPRINT ` | |
-Tenant $env:CDX_TENANTID ` | |
-ErrorAction Stop | |
$tenants = Get-TenantInformation | |
$timestamp = Get-Date -Format FileDateTime | |
foreach( $tenant in $tenants ) | |
{ | |
Connect-PnPOnline -Url $tenant.TenantAdminDomain ` | |
-ClientId $env:CDX_CLIENTID ` | |
-Thumbprint $env:CDX_THUMBPRINT ` | |
-Tenant $env:CDX_TENANTID ` | |
-ErrorAction Stop | |
$tenantContext = Get-PnPContext | |
$o365Tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($tenantContext) | |
$sites = Get-PnPTenantSite | |
$counter = 0 | |
$results = foreach( $site in $sites ) | |
{ | |
$counter++ | |
Write-Host "[$(Get-Date)] - ($counter/$($sites.Count)) - Processing site: $($site.Url)" | |
$administratorEmails = $ownerEmails = @() | |
if( $site.LockState -ne "NoAccess" ) | |
{ | |
# lookup siteId | |
$siteId = [Microsoft.SharePoint.Client.TenantExtensions]::GetSiteGuidByUrl( $o365Tenant, $site.Url ) | |
# use the REST API to query for site administrators | |
$administrators = Invoke-PnPSPRestMethod -Method GET -Url "/_api/Microsoft.Online.SharePoint.TenantAdministration.Tenant/GetSiteAdministrators('$siteId')" -ErrorAction Stop | |
$administratorEmails = $administrators.value.email.Where( { -not [string]::IsNullOrEmpty($_)} ) | |
# use the REST API to query for site owers group | |
$groups = Invoke-PnPSPRestMethod -Method GET -Url "/_api/SPO.Tenant/sites/GetSiteUserGroups?siteId='$siteId'&userGroupIds=[0]" -ErrorAction Stop | |
# pull owner emails | |
$ownerEmails = $groups.value[0].userGroup.email.Where( { -not [string]::IsNullOrEmpty($_)} ) | |
} | |
[PSCustomObject] @{ | |
GeoLocation = $tenant.GeoLocation | |
SiteUrl = $site.Url | |
Title = $site.Title | |
LockState = $site.LockState | |
SiteAdministrators = $administratorEmails -join ";" | |
SiteOwners = $ownerEmails -join ";" | |
Storage = [Math]::Round( $site.StorageUsageCurrent / 1024, 2) | |
} | |
} | |
$results | Export-Csv -Path "sitemetadata_$timestamp.csv" -NoTypeInformation -Append | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment