Last active
April 7, 2023 03:07
-
-
Save neerajks77/fd9cda02d6d3c6508a1fffdee7d9860e to your computer and use it in GitHub Desktop.
Create Microsoft Teams with default channel, add owner, and users
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
param( | |
[Parameter(Mandatory=$false)] | |
[string] $TeamName = 'Test Team', | |
[Parameter(Mandatory=$false)] | |
[string] $TeamDescription = 'New MS Team', | |
[Parameter(Mandatory=$false)] | |
[string] $Visibility = 'Private', | |
[Parameter(Mandatory=$false)] | |
[string] $UPIId = 'neeraj@azure-training.com', | |
[Parameter(Mandatory=$false)] | |
[string[]] $Users | |
) | |
# Enforcing validation of code | |
Set-StrictMode -Version Latest | |
function CreateMSTeamsTeamGAPI{ | |
Connect-AzAccount -Identity | |
$tenantID = '<tenant id>' | |
$clientId = '<client id>' | |
$clientSecret = '<client secret>' | |
try{ | |
$graphtokenBody = @{ | |
Grant_Type = "client_credentials" | |
Scope = "https://graph.microsoft.com/.default" | |
Client_Id = $clientId | |
Client_Secret = $clientSecret | |
} | |
$graphtoken = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token" -Method POST -Body $graphtokenBody | Select-Object -ExpandProperty Access_Token | |
$teamstokenBody = @{ | |
Grant_Type = "client_credentials" | |
Scope = "48ac35b8-9aa8-4d74-927d-1f4a14a0b239/.default" | |
Client_Id = $clientId | |
Client_Secret = $ClientSecret | |
} | |
$teamsToken = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenantID/oauth2/v2.0/token" -Method POST -Body $teamstokenBody | Select-Object -ExpandProperty Access_Token | |
Connect-MicrosoftTeams -AccessTokens @("$graphToken", "$teamsToken") | |
$TeamExists = Get-Team -DisplayName $TeamName | |
If($TeamExists){ | |
write-output "This Team with name $TeamName already Exists. The request has been marked as closed incoleted and a new task has been created" | |
} | |
else{ | |
Connect-AzureAD -TenantId $tenantID -ApplicationId $clientId -CertificateThumbprint $Thumbprint | |
$UPN = Get-AzureADUser -ObjectID $UPIId | |
if ($UPN -ne $null){ | |
$group = New-Team -DisplayName $TeamName -Description $TeamDescription -Visibility $Visibility -Owner $UPIId | |
if((!$Users.count -eq 0) -or (!$Users -eq '')) | |
{ | |
foreach($TeamMember in $Users){ | |
Add-TeamUser -GroupId $group.GroupId -User $TeamMember | |
} | |
} | |
write-output "New Team created with name - " + $TeamName + ". Request has been Completed." | |
} | |
else{ | |
write-output "This Team owner UPN $UPIId does not exists in Azure AD. Please p[rovide a valid owner detail" | |
} | |
} | |
} | |
Catch{ | |
write-output $_.Exception.ToString() | |
} | |
} | |
# Initiate the variable here | |
################################## | |
[string] $Thumbprint = Get-AutomationVariable -Name 'CERT_THUMBPRINT' | |
##################################### | |
# call the function to provision email id | |
##################################### | |
CreateMSTeamsTeamGAPI | |
################################################################# End Main Section ############################################################ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment