Last active
March 1, 2024 20:58
-
-
Save joerodgers/e5569b11aee9267462ae84b37909f37e to your computer and use it in GitHub Desktop.
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
#requires -modules "pnp.powershell" | |
# required permissions: | |
# SharePoint > Sites.FullControl.All | |
Connect-PnPOnline ` | |
-Url "https://$env:O365_TENANT-admin.sharepoint.com" ` | |
-ClientId $env:O365_CLIENTID ` | |
-Thumbprint $env:O365_THUMBPRINT ` | |
-Tenant $env:O365_TENANTID | |
$list = Get-PnpList -Identity "DO_NOT_DELETE_SPLIST_TENANTADMIN_AGGREGATED_SITECOLLECTIONS" -Includes Fields | |
$fields = "LastActivityOn", "NumOfFiles", "PageViews", "PagesVisited", "SensitivityLabel", "SiteId", "SiteUrl", "StorageUsed", "TemplateName", "TimeCreated", "TimeDeleted" | |
$items = Get-PnPListItem -List $list -PageSize 5000 -Fields $fields | |
$results = @() | |
foreach( $item in $items ) | |
{ | |
if( -not [string]::IsNullOrEmpty( $item.FieldValues.TimeDeleted ) ) | |
{ | |
# Write-Host "Skipping deleted site: $($item.FieldValues.SiteUrl)" | |
continue | |
} | |
$result = [PSCustomObject] @{} | |
foreach( $field in $fields ) | |
{ | |
$result | Add-Member -MemberType NoteProperty -Name $field -Value $item.FieldValues[$field] | |
} | |
$result | Add-Member -MemberType NoteProperty -Name "Administrators" -Value "" | |
if( $result.TemplateName -eq "SITEPAGEPUBLISHING#0" ) | |
{ | |
try | |
{ | |
$admins = Invoke-PnPSPRestMethod -Method Get -Url "/_api/Microsoft.Online.SharePoint.TenantAdministration.Tenant/GetSiteAdministrators(siteId='$($item.FieldValues.SiteId)')" | |
$result.Administrators = $admins.value.userPrincipalName -join ";" | |
} | |
catch | |
{ | |
Write-Host "Failed to lookup group or group owners for site: $($result.SiteUrl). Error: $($_)" -ForegroundColor Red | |
} | |
} | |
else | |
{ | |
continue # skip non-communciations sites | |
} | |
$results += $result | |
} | |
$results | Export-Csv -Path "CommunicationSites.csv" -NoTypeInformation |
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
#requires -modules "pnp.powershell" | |
# required permissions: | |
# SharePoint > Sites.FullControl.All | |
# Microsoft Graph > Directory.Read.All | |
Connect-PnPOnline ` | |
-Url "https://$env:O365_TENANT-admin.sharepoint.com" ` | |
-ClientId $env:O365_CLIENTID ` | |
-Thumbprint $env:O365_THUMBPRINT ` | |
-Tenant $env:O365_TENANTID | |
$list = Get-PnpList -Identity "DO_NOT_DELETE_SPLIST_TENANTADMIN_AGGREGATED_SITECOLLECTIONS" -Includes Fields | |
$fields = "GroupId", "LastActivityOn", "NumOfFiles", "PageViews", "PagesVisited", "SensitivityLabel", "SiteId", "SiteUrl", "StorageUsed", "TemplateName", "TimeCreated", "TimeDeleted" | |
$items = Get-PnPListItem -List $list -PageSize 5000 -Fields $fields | |
$results = @() | |
foreach( $item in $items ) | |
{ | |
if( -not [string]::IsNullOrEmpty( $item.FieldValues.TimeDeleted ) ) | |
{ | |
# Write-Host "Skipping deleted site: $($item.FieldValues.SiteUrl)" | |
continue | |
} | |
$result = [PSCustomObject] @{} | |
foreach( $field in $fields ) | |
{ | |
$result | Add-Member -MemberType NoteProperty -Name $field -Value $item.FieldValues[$field] | |
} | |
$result | Add-Member -MemberType NoteProperty -Name "Visibility" -Value "" | |
$result | Add-Member -MemberType NoteProperty -Name "GroupOwners" -Value "" | |
if( $result.GroupId -ne "00000000-0000-0000-0000-000000000000" ) | |
{ | |
try | |
{ | |
$m365Group = Get-PnPMicrosoft365Group -Identity $result.GroupId -ErrorAction Stop | |
$result.Visibility = $m365Group.Visibility | |
$m365GroupOwners = Get-PnPMicrosoft365GroupOwner -Identity $result.GroupId -ErrorAction Stop | |
$result.GroupOwners = $m365GroupOwners.UserPrincipalName -join ";" | |
} | |
catch | |
{ | |
Write-Host "Failed to lookup group or group owners for site: $($result.SiteUrl). Error: $($_)" -ForegroundColor Red | |
} | |
} | |
else | |
{ | |
continue # skip anything not group connected | |
} | |
$results += $result | |
} | |
$results | Export-Csv -Path "GroupSites.csv" -NoTypeInformation | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment