Created
March 5, 2019 09:53
-
-
Save AlexanderHolmeset-zz/59e49637e29798ea61ec2525956a6856 to your computer and use it in GitHub Desktop.
Create new Teams team
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= $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