Skip to content

Instantly share code, notes, and snippets.

@joerodgers
Last active June 21, 2024 15:36
Show Gist options
  • Save joerodgers/abe2eab302f5220461fb7233ec3cf605 to your computer and use it in GitHub Desktop.
Save joerodgers/abe2eab302f5220461fb7233ec3cf605 to your computer and use it in GitHub Desktop.
Shows all M365 group connected sites and reports if the associated M365 group exists.
#requires -module "PnP.PowerShell"
# Required permissions:
# Microsoft Graph API : One of Directory.Read.All, Directory.ReadWrite.All, Group.Read.All, Group.ReadWrite.All, GroupMember.Read.All, GroupMember.ReadWrite.All
# SharePoint Online : 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 `
-ErrorAction Stop
$sites = Get-PnPTenantSite -GroupIdDefined $true -ErrorAction Stop
$results = foreach( $site in $sites )
{
Write-Host "[$(Get-date)] - Scanning site: $($site.Url)"
if( $site.GroupId -ne [Guid]::Empty )
{
$groupId = $site.GroupId
}
else
{
$groupId = $site.RelatedGroupId
}
try
{
$group = Get-PnPMicrosoft365Group -Identity $groupId -ErrorAction Stop
}
catch
{
Write-Error "Failed to lookup group: $groupId. Error: $_"
}
[PSCustomObject] @{
SiteUrl = $site.Url
GroupId = $site.GroupId
RelatedGroupId = $site.RelatedGroupId
GroupExists = $null -ne $group
}
}
$results | Export-Csv -Path "M365GroupConnectionExists.csv" -NoTypeInformation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment