Last active
August 26, 2022 08:31
-
-
Save mozziemozz/da5e36678540355993ff93abde3f8bba to your computer and use it in GitHub Desktop.
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
| # Author: Martin Heusser (@mozzeph) | |
| # Required Scopes: "profile", "openid", "User.Read.All", "Chat.Create" | |
| $AppId = '' | |
| $AppSecret = '' | |
| $TenantName = '' | |
| function Connect-HTTPGraph { | |
| param ( | |
| [Parameter(Mandatory=$true)][String]$AppId, | |
| [Parameter(Mandatory=$true)][String]$AppSecret, | |
| [Parameter(Mandatory=$true)][String]$TenantName | |
| ) | |
| # Define AppId, secret and scope, your tenant name and endpoint URL | |
| $AppId = $AppId | |
| $AppSecret = $AppSecret | |
| $Scope = "https://graph.microsoft.com/.default" | |
| $TenantName = $TenantName | |
| $Url = "https://login.microsoftonline.com/$TenantName/oauth2/v2.0/token" | |
| # Add System.Web for urlencode | |
| Add-Type -AssemblyName System.Web | |
| # Create body | |
| $Body = @{ | |
| client_id = $AppId | |
| client_secret = $AppSecret | |
| scope = $Scope | |
| grant_type = 'client_credentials' | |
| } | |
| # Splat the parameters for Invoke-Restmethod for cleaner code | |
| $PostSplat = @{ | |
| ContentType = 'application/x-www-form-urlencoded' | |
| Method = 'POST' | |
| # Create string by joining bodylist with '&' | |
| Body = $Body | |
| Uri = $Url | |
| } | |
| # Request the token! | |
| $Request = Invoke-RestMethod @PostSplat | |
| # Create header | |
| $Header = @{ | |
| Authorization = "$($Request.token_type) $($Request.access_token)" | |
| } | |
| } | |
| . Connect-HTTPGraph -AppId $AppId -AppSecret $AppSecret | |
| # Get all Teams resource accounts | |
| $allResourceAccounts = (Invoke-RestMethod -Method Get -Uri "https://graph.microsoft.com/v1.0/users?`$filter=startsWith(department,'Microsoft Communication Application Instance')" -Headers $Header -ContentType "application/json").value | |
| # Example to get all users licensed for Teams Phone | |
| $allTeamsPhoneUsers = (Invoke-RestMethod -Method Get -Uri "https://graph.microsoft.com/v1.0/users?`$filter=assignedLicenses/any(a:a/skuId eq e43b5b99-8dfb-405f-9987-dc307f34bcbd)" -Headers $Header -ContentType "application/json").value | |
| # Test with only 1 user | |
| #$allTeamsPhoneUsers = $allTeamsPhoneUsers[-1] | |
| foreach ($user in $allTeamsPhoneUsers) { | |
| $userId = $user.id | |
| foreach ($resourceAccount in $allResourceAccounts) { | |
| Write-Host $resourceAccount.DisplayName | |
| $resourceAccountId = $resourceAccount.id | |
| $createChat = @" | |
| { | |
| "chatType": "oneOnOne", | |
| "members": [ | |
| { | |
| "@odata.type": "#microsoft.graph.aadUserConversationMember", | |
| "roles": ["owner"], | |
| "[email protected]": "https://graph.microsoft.com/v1.0/users('$userId')" | |
| }, | |
| { | |
| "@odata.type": "#microsoft.graph.aadUserConversationMember", | |
| "roles": ["owner"], | |
| "[email protected]": "https://graph.microsoft.com/v1.0/users('$resourceAccountId')" | |
| } | |
| ] | |
| } | |
| "@ | |
| (Invoke-RestMethod -Method Post -Uri "https://graph.microsoft.com/v1.0/chats" -Headers $Header -ContentType "application/json" -Body $createChat) | |
| #Read-Host | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment