Skip to content

Commit

Permalink
Merge pull request #597 from HeinrichKrause/patch-1
Browse files Browse the repository at this point in the history
Minor updates to PS coding conventions
  • Loading branch information
pkbullock authored Oct 18, 2023
2 parents e2da75a + c0b3845 commit 327f977
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 34 deletions.
66 changes: 33 additions & 33 deletions scripts/teams-archive-inactive-teams/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ v1.0 - 8/6/2023
#>
Function Archive-PnPInactiveTeams {
<#
<#
.SYNOPSIS
Script to archive the inactive Teams
Expand All @@ -45,85 +45,84 @@ This solution requires an Azure App registration with the following permissions:
- Reports.Read.All
- TeamSettings.ReadWrite.All
.Parameter tenandId
.Parameter TenandId
The ID of your tenant
.PARAMETER clientId
.PARAMETER ClientId
The ID of your Azure app registration
.PARAMETER clientSecret
.PARAMETER ClientSecret
The secret of your Azure app registration
.PARAMETER inactiveDays
.PARAMETER InactiveDays
The minimum number of days that a Team must be active in order to be archived otherwise. Possible values: 7, 30, 90 or 180
.Example
Archive-PnPInactiveTeams -tenandId "XXXXXX" -clientId "XXXXXX" -clientSecret "XXXXXX" -inactiveDays 30
Archive-PnPInactiveTeams -TenandId "XXXXXX" -ClientId "XXXXXX" -ClientSecret "XXXXXX" -InactiveDays 30
.Example
Archive-PnPInactiveTeams -tenandId "XXXXXX" -clientId "XXXXXX" -clientSecret "XXXXXX" -inactiveDays 180
Archive-PnPInactiveTeams -TenandId "XXXXXX" -ClientId "XXXXXX" -ClientSecret "XXXXXX" -InactiveDays 180
#>
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
$tenantId,
$TenantId,
[Parameter(Mandatory = $true)]
$clientId,
$ClientId,
[Parameter(Mandatory = $true)]
$clientSecret,
$ClientSecret,
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[ValidateSet("7","30","90","180")]
$inactiveDays
[ValidateSet("7", "30", "90", "180")]
$InactiveDays
)
begin {
#Log in to Microsoft Graph
Write-Host "Connecting to Microsoft Graph" -f Yellow
Write-Host "Connecting to Microsoft Graph" -ForegroundColor Yellow
$uri = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"
$uri = "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token"
$body = @{
client_id = $clientId
client_secret = $clientSecret
grant_type = "client_credentials"
scope = "https://graph.microsoft.com/.default"
client_id = $ClientId
client_secret = $ClientSecret
grant_type = "client_credentials"
scope = "https://graph.microsoft.com/.default"
}
$response = invoke-restMethod -Uri $uri -method Post -body $body
$response = Invoke-RestMethod -Uri $uri -Method Post -Body $body
$accessToken = $response.access_token
}
process {
$today = Get-Date
# Get the Teams Team activity detail
$inactiveTeamsUri = "https://graph.microsoft.com/v1.0/reports/getTeamsTeamActivityDetail(period='D$inactiveDays')"
$inactiveTeamsUri = "https://graph.microsoft.com/v1.0/reports/getTeamsTeamActivityDetail(period='D$InactiveDays')"
$inactiveTeamsHeader = @{
Authorization = "Bearer $accessToken"
}
$inactiveTeamsResponse = invoke-restMethod -Uri $inactiveTeamsUri -method Get -Headers $inactiveTeamsHeader
$inactiveTeamsResponse = Invoke-RestMethod -Uri $inactiveTeamsUri -Method Get -Headers $inactiveTeamsHeader
$teams = $inactiveTeamsResponse | ConvertFrom-Csv | where {$_.'Last Activity Date' -ne ""}
$teams = $inactiveTeamsResponse | ConvertFrom-Csv | Where-Object { $_.'Last Activity Date' -ne "" }
foreach($team in $teams) {
foreach ($team in $teams) {
$lastActivityDate = $team.'Last Activity Date'
$timeSpan = New-TimeSpan -Start $lastActivityDate -End $today
if($timeSpan.Days -gt $inactiveDays){
if ($timeSpan.Days -gt $InactiveDays) {
$teamId = $team.'Team Id'
$teamName = $team.'Team Name'
Write-Host "Team $teamName ($teamId) is inactive since $($timeSpan.Days) days" -f DarkYellow
Write-Host "Team $teamName ($teamId) is inactive since $($timeSpan.Days) days" -ForegroundColor DarkYellow
$archiveTeamUri = "https://graph.microsoft.com/v1.0/teams/$teamId/archive"
invoke-restMethod -Uri $archiveTeamUri -method Post -Headers $inactiveTeamsHeader
Write-Host "Team $teamName ($teamId) is archived" -f Green
Invoke-RestMethod -Uri $archiveTeamUri -Method Post -Headers $inactiveTeamsHeader
Write-Host "Team $teamName ($teamId) is archived" -ForegroundColor Green
}
}
}
end {
}
}
Expand All @@ -134,6 +133,7 @@ Archive-PnPInactiveTeams -tenandId "XXXXXX" -clientId "XXXXXX" -clientSecret "XX
| Author(s) |
|-----------|
| [Nico De Cleyre](https://www.nicodecleyre.com)|
| Heinrich Krause |


[!INCLUDE [DISCLAIMER](../../docfx/includes/DISCLAIMER.md)]
Expand Down
7 changes: 6 additions & 1 deletion scripts/teams-archive-inactive-teams/assets/sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"By inputting a designated timeframe for inactivity, the script automatically identifies Teams that have remained dormant beyond the specified period. These Teams are then archived."
],
"creationDateTime": "2023-08-06",
"updateDateTime": "2023-08-06",
"updateDateTime": "2023-10-13",
"products": [
"Teams"
],
Expand All @@ -32,6 +32,11 @@
}
],
"authors": [
{
"gitHubAccount": "HeinrichKrause",
"pictureUrl": "https://github.com/HeinrichKrause.png",
"name": "Heinrich Krause"
},
{
"gitHubAccount": "nicodecleyre",
"pictureUrl": "https://avatars.githubusercontent.com/u/35696168?v=4",
Expand Down

0 comments on commit 327f977

Please sign in to comment.