Create a Teams Appointment with MS GRAPH API and Powershell

Recently, I created an Azure Automation Runbook to keep my Notions Tasks in Sync with my Calendar. I used Graph API to create and update my Outlook Events.

From this, I also tried to send a Teams Appointment Invite with Powershell and Graph API. The result is below.

  • Application
    • Calendars.ReadWrite

More Details here: Create event – Microsoft Graph v1.0 | Microsoft Learn

The Script

Here is the full Script with comments. Please remember that the scripts are maintained on GitHub, so I recommend navigating to the latest version of GitHub.

$clientID = "your ClientID"
$Clientsecret = "Your Secret"
$tenantID = "your TenantID"

$Graph_BaseURL = "https://graph.microsoft.com/v1.0"


#Calendar User
$TargetUser="michael.seidl@au2mator.com"


#Invite User
$RecipientMail="ahmed.uzejnovic@au2mator.com"
$RecipientName="Ahmed Uzejnovic"
$RecipientType="required" #required, optional



#Teams Event Details
$TeamsEventSubject = "My Teams-GRAPH API Event"
$TeamsEventBody = "Thats my awesome Teams-GRAPH API Event Body"

$TeamsEventStart = "2024-02-25T09:00:00"
$TeamsEventEnd = "2024-02-25T10:00:00"
$timeZone = "UTC"



#Authentication
#Connect to GRAPH API
$tokenBody = @{
    Grant_Type    = "client_credentials"
    Scope         = "https://graph.microsoft.com/.default"
    Client_Id     = $clientId
    Client_Secret = $clientSecret
}
$tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token" -Method POST -Body $tokenBody
$headers = @{
    "Authorization" = "Bearer $($tokenResponse.access_token)"
    "Content-type"  = "application/json"
}



$TeamsEventJson = @"
{
  "subject": "$TeamsEventSubject",
  "isOnlineMeeting": true,
  "onlineMeetingProvider": "teamsForBusiness",
  "attendees": [
    {
      "emailAddress": {
        "address":"$RecipientMail",
        "name": "$RecipientName"
      },
      "type": "$RecipientType"
    }
  ],
  "body": {
    "contentType": "HTML",
    "content": "$($TeamsEventBody)"
  },
  "start": {
      "dateTime": "$($TeamsEventStart)",
      "timeZone": "$timeZone"
  },
  "end": {
      "dateTime": "$($TeamsEventEnd)",
      "timeZone": "$timeZone"
  }
}
"@


$TeamsEvent = Invoke-RestMethod -Uri "$Graph_BaseURL/users/$TargetUser/calendar/events" -Method POST -Headers $headers -Body $TeamsEventJson -ContentType "application/json; charset=utf-8"

The Result

Github

Make sure to get the latest version from GitHub: Seidlm/Microsoft-Teams (github.com) (New Teams Invite.ps1)

Michael Seidl aka Techguy
au2mate everything
#AutomationMindset

Leave a Comment

Your email address will not be published. Required fields are marked *

*