Installing the Microsoft Graph Modules
##Add Repopsitory
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine -Force;
Install-PackageProvider -Name NuGet -Force;
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted;
##Install modules
Install-Module Microsoft.Graph -Scope CurrentUser -Force
Install-Module MSAL.PS -Scope AllUsers -Force
Clone a single Team
# Input Parameters
$clientId = "5b7c3c0d-f249-4236-9af9-449b704c0a1d"
$clientSecret = "AQK7Q~X9eYxwaq7tL3BzwojiqNMp26WiUxFFd"
$tenantName = "leoguides.onmicrosoft.com"
$GroupId = "ae050580-90dc-4c8f-b22a-04a107a2b58e"
$URL = "https://graph.microsoft.com/v1.0/teams/$GroupId/clone"
$tokenBody = @{
Grant_Type = "client_credentials"
Scope = "https://graph.microsoft.com/.default"
Client_Id = $clientId
Client_Secret = $clientSecret
}
$body = @’
{
"displayName": "Cloned Team by LeoGuides",
"description": "Cloned Team using Graph API",
"mailNickname": "clonedTeam",
"partsToClone": "apps,tabs,settings,channels,members",
"visibility": "public"
}
‘@
$tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenantName/oauth2/v2.0/token" -Method POST -Body $tokenBody
Invoke-RestMethod -Headers @{Authorization = "Bearer $($tokenResponse.access_token)"} -Uri $URL -Method POST -Body $body -ContentType 'application/json'
Clone to multiple Teams using Csv file
Create a Csv file C:\Temp\teams.csv
#Input Parameters
$ClientId = "5b7c3c0d-f249-4236-9af9-449b704c0a1d"
$ClientSecret = "AQK7Q~X9eYxwaq7tL3BzwojiqNMp26WiUxFFd"
$TenantName = "leoguides.onmicrosoft.com"
$GroupId = "ae050580-90dc-4c8f-b22a-04a107a2b58e"
$URL = "https://graph.microsoft.com/v1.0/teams/$GroupId/clone"
$tokenBody = @{
Grant_Type = "client_credentials"
Scope = "https://graph.microsoft.com/.default"
Client_Id = $clientId
Client_Secret = $clientSecret
}
$tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenantName/oauth2/v2.0/token" -Method POST -Body $tokenBody
#Clone
Import-Csv C:\Temp\teams.csv | Foreach {
$DisplayName=$_.DisplayName
$Description=$_.Description
$MailNickname=$_.MailNickname
$visibility=$_.visibility
$partsToClone= "apps,tabs,settings,channels,members" #Add or Remove what does not need to be cloned
Write-host -f Yellow "Cloning Team: $GroupId to new Team: '$($_.DisplayName)'"
$body = @"
{
"displayName": `"$DisplayName`",
"description": `"$Description`",
"mailNickname": `"$MailNickname`",
"partsToClone": `"$partsToClone`",
"visibility" : `"$visibility`"
}
"@
Invoke-RestMethod -Headers @{Authorization = "Bearer $($tokenResponse.access_token)"} -Uri $URL -Method POST -Body $body -ContentType 'application/json'
}
5/5 - (1 vote)