Create an Outlook 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.

So, we need Powershell and the Following GRAPH API Permission for our Script.

  • 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 Client ID"
$Clientsecret = "your Secret"
$tenantID = "your Tenant ID"

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


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


#Simple Event Details
$EventSubject = "My GRAPH API Event"
$EventBody = "Thats my awesome GRAPH API Event Body"
$EventLocation = "Microsoft Headquarter"

$EventStart = "2024-02-10T09:00:00"
$EventEnd = "2024-02-10T10: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"
}



$SimpleEventJson = @"
{
  "subject": "$EventSubject",
  "location":{
    "displayName":"$EventLocation",
    "address":{
        "street":"4567 Main St",
        "city":"Redmond",
        "state":"WA",
        "countryOrRegion":"US",
        "postalCode":"32008"
      },
},
  "body": {
    "contentType": "HTML",
    "content": "$($EventBody)"
  },
  "start": {
      "dateTime": "$($EventStart)",
      "timeZone": "$timeZone"
  },
  "end": {
      "dateTime": "$($EventEnd)",
      "timeZone": "$timeZone"
  }
}
"@

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



The Result

Github

Make sure to get the latest version from GitHub: Seidlm/Microsoft-Graph-API-Examples (github.com) (New Outlook Appointment.ps1)

Michael Seidl aka Techguy
au2mate everything
#AutomationMindset

Leave a Comment

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

*