Created
April 24, 2020 00:13
-
-
Save JeffBrownTech/8e80ead92fd2203848e30d93e0c5221d to your computer and use it in GitHub Desktop.
Create M365 Group, Team, and Channel in PowerShell Using Microsoft Graph API
This file contains hidden or 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
# Configure app registration and tenant information | |
$env:graphApiDemoAppId = "12345678-abcd-efgh-jklm-123456789abc" # Replace with your Azure AD app id | |
$env:graphApiDemoAppSecret = "1234567890asdfjk;l54321" # Replace with your Azure AD app secret | |
$env:tenantId = "12345678-abcd-efgh-ijkl-987654321wxyz" # Replace with your Azure AD tenant ID | |
$oauthUri = "https://login.microsoftonline.com/$env:tenantId/oauth2/v2.0/token" | |
# Create token request body | |
$tokenBody = @{ | |
client_id = $env:graphApiDemoAppId | |
client_secret = $env:graphApiDemoAppSecret | |
scope = "https://graph.microsoft.com/.default" | |
grant_type = "client_credentials" | |
} | |
# Retrieve access token | |
$tokenRequest = Invoke-RestMethod -Uri $oauthUri -Method POST -ContentType "application/x-www-form-urlencoded" -Body $tokenBody -UseBasicParsing | |
# Save access token value | |
$accessToken = ($tokenRequest).access_token | |
# Set request headers with access token and content-type | |
# This will be used in each request to the Graph service as long as the $accessToken is valid | |
$headers = @{ | |
"Authorization" = "Bearer $accessToken" | |
"Content-type" = "application/json" | |
} | |
# Create request body with M365 group properties | |
$groupBody = | |
'{ | |
"displayName": "Team from Graph API Demo", | |
"mailNickname": "teamfromgraphapidemo", | |
"description": "Demo making a group from Graph API", | |
"[email protected]": [ | |
"https://graph.microsoft.com/v1.0/users/{id}" # Use object ID or UPN of user | |
], | |
"groupTypes": [ | |
"Unified" | |
], | |
"mailEnabled": "true", | |
"securityEnabled": "false", | |
"visibility": "Private" | |
}' | |
# Invoke request again Graph service to create group | |
$newGroup = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/groups" -Method POST -Headers $headers -Body $groupBody | |
# Create the request body with team properties | |
$teamBody = | |
'{ | |
"memberSettings": { | |
"allowCreateUpdateChannels": true, | |
"allowDeleteChannels": true, | |
"allowAddRemoveApps": true, | |
"allowCreateUpdateRemoveTabs": true, | |
"allowCreateUpdateRemoveConnectors": true | |
}, | |
"guestSettings": { | |
"allowCreateUpdateChannels": true, | |
"allowDeleteChannels": true | |
}, | |
"messagingSettings": { | |
"allowUserEditMessages": true, | |
"allowUserDeleteMessages": true, | |
"allowOwnerDeleteMessages": true, | |
"allowTeamMentions": true, | |
"allowChannelMentions": true | |
}, | |
"funSettings": { | |
"allowGiphy": true, | |
"giphyContentRating": "strict", | |
"allowStickersAndMemes": true, | |
"allowCustomMemes": true | |
} | |
}' | |
# Invoke request again Graph service to create team based on the previously created M365 groups | |
$newTeam = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/groups/$($newGroup.id)/team" -Method PUT -Headers $headers -Body $teamBody | |
# Create the request body with channel properties | |
$channelBody = | |
'{ | |
"displayName": "Channel from Graph API", | |
"description": "Demo how to make a channel using graph api" | |
}' | |
# Invoke request against Graph service to create a channel in the previously created team | |
$newChannel = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/teams/$($newTeam.id)/channels" -Method POST -Headers $headers -Body $channelBody |
Check out this link, looks like you might be missing the "@odata.type": "#Microsoft.Graph.channel" property.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, first of all congratulations on your work.
But I have a question, how do you create a private channel in Teams?
I'm trying but is impossible for me.
`
$PrivateChannel = @{
displayname = 'test2'
description = 'test2'
membershiptype = 'private'
members = @{
'@odata.type = '#microsoft.graph.aaduserconverstionmember'
'[email protected]' = 'https://graph.microsoft.com/beta/users($IdUser)
roles = 'owner'
}
}
$PrivateChannelJson = ($PrivateChannel | convertto-json)
Invoke-RestMethod -Headres $authHeader -Uri "https://graph.microsoft.com/beta/teams/$IdTeams/channels" -Method Post -ContentType 'application/json' -Body $PrivateChannelJson
`
And the answer is Error: (400) Bad Request