Created
February 19, 2024 18:50
-
-
Save neerajks77/f707c5ec56b9d0a0a30af114253c31d5 to your computer and use it in GitHub Desktop.
Extract the details of Microsoft Teams that includes Site Team and channel details, storage space, owner details, etc.
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
##################################################################################### | |
######Author: Neeraj Kumar | |
######Created: 28/09/2023 | |
######Usage: Extract the details of Microsoft Teams that includes Site Team and channel details, storage space, owner details, etc. | |
##################################################################################### | |
# Variables from Azure Automation Account | |
$tenantId = Get-AutomationVariable -Name 'TenantId' | |
$clientId = Get-AutomationVariable -Name 'ClientId' | |
$clientSecret = Get-AutomationVariable -Name 'ClientSecret' | |
$spSiteUrl = Get-AutomationVariable -Name 'SharePointSiteUrl' | |
$spSiteName = Get-AutomationVariable -Name 'SharePointSiteName' | |
$spLibraryName = Get-AutomationVariable -Name 'TeamsDocumentLibraryName' | |
# Function to get token | |
function ConnecttoGraph { | |
$tokenUrl = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token" | |
$tokenBody = @{ | |
client_id = $clientId | |
scope = "https://graph.microsoft.com/.default" | |
client_secret = $clientSecret | |
grant_type = "client_credentials" | |
} | |
$tokenResponse = Invoke-RestMethod -Method Post -Uri $tokenUrl -Body $tokenBody | |
$global:accessToken = @{'Authorization'="$($tokenResponse.token_type) $($tokenResponse.access_token)"} | |
} | |
function GetTeamDetails{ | |
# $headers = @{ | |
# "Authorization" = "Bearer $Global:tokenResponse" | |
# "Content-Type" = "application/json" | |
# } | |
# Get all Teams | |
#$teams = Invoke-RestMethod -Headers $global:accessToken -Uri "https://graph.microsoft.com/v1.0/groups?$top=999&$filter=groupTypes/any(c:c+eq+'Unified')" | |
$teams = Invoke-RestMethod -Headers $global:accessToken -Uri 'https://graph.microsoft.com/v1.0/teams?$top=999' #"https://graph.microsoft.com/v1.0/groups?$filter=resourceProvisioningOptions/Any(x:x eq 'Team')" | |
#$groupDetails = Invoke-RestMethod -Headers $global:accessToken -Uri "https://graph.microsoft.com/v1.0/groups?$filter=resourceProvisioningOptions/Any(x:x eq 'Team')" | |
Write-Output "Teams Count - " + $teams.value.count | |
$Global:teamDetails = @() | |
foreach ($team in $teams.value) { | |
$teamId = $team.id | |
# Fetch channels | |
$channels = Invoke-RestMethod -Headers $global:accessToken -Uri "https://graph.microsoft.com/v1.0/teams/$teamId/channels" | |
# Fetch members | |
$members = Invoke-RestMethod -Headers $global:accessToken -Uri "https://graph.microsoft.com/v1.0/teams/$teamId/members" | |
# Fetch Group Details | |
$group = Invoke-RestMethod -Headers $global:accessToken -Uri "https://graph.microsoft.com/v1.0/groups/$teamId" | |
$groupid = $group.id | |
# Other details can be fetched similarly | |
$owner = Invoke-RestMethod -Headers $global:accessToken -Uri "https://graph.microsoft.com/v1.0/groups/$groupid/owners" | |
$Global:teamDetails += [PSCustomObject]@{ | |
"Team ID" = $teamId | |
"Team Name" = $team.displayName | |
"Owner" = ($owner.value).displayName -join ',' | |
"Owner Count" = ($owner.value).count | |
"Number of Channels" = ($channels.value).count | |
"Members" = ($members.value).displayName -join ', ' | |
"Member Count" = ($members.value).count | |
"Classification" = $group.Classification | |
"Created On" = $group.createdDateTime | |
#"Creation Options" = $team.creationOptions | |
"SharePoint Site Language" = $team.SPSiteLanguage | |
"Description" = $team.description | |
"Expiration Date Time" = $group.expirationDateTime | |
"Group Types" = $group.groupTypes[0] | |
# "Is Assignable to Role" = $team.isAssignableToRole | |
"Team Email" = $group.mail | |
"Email Enabled" = $group.mailEnabled | |
"Email Nickname/Alias" = $group.mailNickname | |
"Membership Rule" = $group.membershipRule | |
"Membership Rule Processing State" = $group.membershipRuleProcessingState | |
"Renewal Date and Time" = $group.renewedDateTime | |
"Deleted On" = $group.deletedDateTime | |
"Security Enabled" = $group.securityEnabled | |
"Theme" = $group.Theme | |
"Visibility" = $team.visibility | |
# ... other properties | |
} | |
#$Global:teamDetails += $teamDetail | |
} | |
write-output $Global:teamDetails | |
WritetoSharePoint | |
} | |
# Authenticate to SharePoint | |
function ConnectToSharePoint | |
{ | |
try{ | |
$spCredentials = Get-AutomationPSCredential -Name "SharePoint" | |
Connect-PnPOnline -Url "$spSiteUrl/sites/$spSiteName" -Credential $spCredentials | |
write-output "inside connect to sharepoint: $spSiteUrl/sites/$spSiteName" | |
} | |
catch{Write-Host "Unable to connect to SharePoint Online.."} | |
} | |
function ExecuteReportAPI | |
{ | |
param([String[]] $Param ) | |
$CSVFileName = $Param[0] | |
$graphApiUri = $Param[1] | |
# $headers = @{ | |
# "Authorization" = "$(Bearer $global:tokenResponse)" | |
# "Content-Type" = "application/json" | |
# } | |
Write-Output $global:accessToken | |
$filePath = $env:Temp | |
try{ | |
Write-Output "Fetching report output by executing Graph API" | |
$Reports = Invoke-RestMethod -Method Get -Uri $graphApiUri -Headers $global:accessToken | ConvertFrom-Csv | |
$Reports | Export-Csv $filePath\$CSVFileName -NoTypeInformation | |
$Values = @{"Title" = 'Team Reports'} | |
# Add the file to the Reports folder | |
Write-Output " Upload file to sharePoint" | |
$RelativeURL =$spLibraryName | |
$FolderObject = Get-PnPFolder -Url $RelativeURL | |
$Upload = Add-PnPFile -Path $filePath\$CSVFileName -Folder $FolderObject | |
If ($Upload -ne $null) | |
{ | |
Write-Output "Report sucessfully uploaded" | |
} | |
} | |
catch | |
{ | |
Write-Error $Error[0] | |
Write-Output "Exception while fetching Teams usage report" | |
} | |
} | |
# Check if the file exists | |
function WritetoSharePoint{ | |
$csvFileName = Get-AutomationVariable -Name 'TeamsReport' | |
write-output $csvFileName | |
# Export to CSV locally | |
$tempFilePath = "$env:TEMP\1.$csvFileName" | |
$Global:teamDetails | Export-Csv -Path $tempFilePath -NoTypeInformation | |
# Upload to SharePoint | |
$FolderObject = Get-PnPFolder -Url "$spSiteUrl/sites/$spSiteName/$spLibraryName" | |
Add-PnPFile -Path $tempFilePath -Folder $FolderObject | |
# Clean up local file | |
Remove-Item -Path $tempFilePath -Force | |
} | |
function main{ | |
ConnecttoGraph | |
ConnectToSharePoint | |
GetTeamDetails | |
# Microsoft Teams Team Activity Report | |
ExecuteReportAPI("4.Teams Activity Counts_30.csv","https://graph.microsoft.com/v1.0/reports/getTeamsTeamActivityCounts(period='D30')") | |
ExecuteReportAPI("4.Teams Activity Distribution Counts_30.csv","https://graph.microsoft.com/v1.0/reports/getTeamsTeamActivityDistributionCounts(period='D30')") | |
ExecuteReportAPI("2.Number of Team Counts_30.csv","https://graph.microsoft.com/v1.0/reports/getTeamsTeamCounts(period='D30')") | |
ExecuteReportAPI("3.Team Activity Details_30.csv","https://graph.microsoft.com/v1.0/reports/getTeamsTeamActivityDetail(period='D30')") | |
# Microsoft Teams user activity by user | |
ExecuteReportAPI("5.Teams User Activity By User_30.csv","https://graph.microsoft.com/v1.0/reports/getTeamsUserActivityUserDetail(period='D30')") | |
#Microsoft Teams activity by activity type count | |
ExecuteReportAPI("6.Teams Activity By Activity Type Counts_30.csv","https://graph.microsoft.com/v1.0/reports/getTeamsUserActivityCounts(period='D30')") | |
#Microsoft Teams user by activity type count | |
ExecuteReportAPI("7.Teams User By Activity Type Counts_30.csv","https://graph.microsoft.com/v1.0/reports/getTeamsUserActivityUserCounts(period='D30')") | |
#Microsoft Teams device usage by user | |
ExecuteReportAPI("8.Teams Device Usage by User_30.csv","https://graph.microsoft.com/v1.0/reports/getTeamsDeviceUsageUserDetail(period='D30')") | |
#Microsoft Teams daily unique users by device type count | |
ExecuteReportAPI("9.Teams Daily Unique User by Device Type Count_30.csv","https://graph.microsoft.com/v1.0/reports/getTeamsDeviceUsageUserCounts(period='D30')") | |
#Microsoft Teams unique users by device type count | |
ExecuteReportAPI("10.Teams Unique User by Device Type Count_30.csv","https://graph.microsoft.com/v1.0/reports/getTeamsDeviceUsageDistributionUserCounts(period='D30')") | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment