Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create new Teams team
Param(
[Parameter (Mandatory= $true)]
[String] $teamname,
[Parameter (Mandatory= $true)]
[String] $owner
)
# Azure AD OAuth Application Token for Graph API
# Get OAuth token for a AAD Application (returned as $token)
# Application (client) ID, tenant ID and secret
#Application (client) ID, tenant ID and secret
$clientId = "623a6fcb-1980-440d-818b-bc851b773919"
$tenantId = "29594fb0-53f2-490c-a1be-8b2cca824805"
$clientSecret = Get-AutomationVariable -Name 'clientsecret'
# Contruct URI
$uri = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"
# Construct Body
$body1 = @{
client_id = $clientId
scope = "https://graph.microsoft.com/.default"
client_secret = $clientSecret
grant_type = "client_credentials"
}
# Get OAuth 2.0 Token
$tokenRequest = Invoke-WebRequest -Method Post -Uri $uri -ContentType "application/x-www-form-urlencoded" -Body $body1 -UseBasicParsing
$token = ($tokenRequest.Content | ConvertFrom-Json).access_token
#Get ID of team requester and set as owner.
$uri = 'https://graph.microsoft.com/beta/users/'+"$owner"+'?$select=id'
$method = "GET"
$query = Invoke-WebRequest -Method $method -Uri $uri -ContentType "application/json" -Headers @{Authorization = "Bearer $token"} -ErrorAction Stop -UseBasicParsing
$ownerID = ($query.content | ConvertFrom-Json).id
# Specify the URI to call and method
$uri = "https://graph.microsoft.com/beta/teams"
$method = "POST"
$body = @"
{
"template@odata.bind": "https://graph.microsoft.com/beta/teamsTemplates/standard",
"displayName": "$teamname",
"description": "My Sample Team’s Description",
"owners@odata.bind": [
"https://graph.microsoft.com/beta/users('$ownerID')"
]
}
"@
# Run Graph API query
$query = Invoke-WebRequest -Method $method -Uri $uri -ContentType "application/json" -Body $body -Headers @{Authorization = "Bearer $token"} -ErrorAction Stop -UseBasicParsing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment