Search code examples
powershelloutlookcalendarmicrosoft-graph-api

Microsoft GraphAPI / Powershell: How do i create multiple PUTS with Graph over Powershell?


i am basically new with the GraphAPI (and "coding" in general) and i have a task i need to accomplish. I need to add multiple Events on other Users Calendars over Powershell. I have managed to enter one event to one users calendar. But i cant get past the one event and add more of them...i did read about the batching with the GraphAPI, but i dont get it.

 #Variables
$ClientID = "xxx"
$ClientSecret = "xxx"
$tenantID = "xxx"
$scope = "https://graph.microsoft.com/.default"
$authority = "https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token"

$Body = @{
  "grant_type"    = "client_credentials";
  "client_id"     = "$ClientID";
  "client_secret" = "$ClientSecret";
  "scope"      = "$scope";
}

#Get AccessToken
$result = Invoke-RestMethod -Method POST -uri $authority -Body $body
$AccessToken = $result.access_token

#Create Event
$mailbox = "[email protected]"
$headers = @{
 "Authorization" = "Bearer "+ $AccessToken;
 "Prefer" = 'outlook.timezone="W. Europe Standard Time"';
 "Content-type"  = "application/json"
}

#Create JSON Object
$json = @"
{
  "subject":"Event over Graph API",
  "body": {
    "contentType" : "HTML",
    "content" : "Write Graph API Powershell Script"
  },
  "start": {
      "dateTime" : "2021-07-23T00:00:00",
      "timeZone" : "W. Europe Standard Time"
  },
  "end": {
      "dateTime" : "2021-07-24T00:00:00",
      "timeZone" : "W. Europe Standard Time"
  },
  "location":{
      "displayName" : "HomeOffice"
  }
}
"@

$uri = "https://graph.microsoft.com/v1.0/users/$Mailbox/calendar/events"
Invoke-RestMethod -Method POST -Uri $uri -Headers $headers -Body $json 

I just found this online and tested it so, S/O to the creator.

Thank you in Advance for any help or input.


Solution

  • With the help Powershell Function you could invoke specific piece code multiple times - make it operate with dynamic objects i.e - same piece of code operate on different object value.

    You create a function update-userevent and have definition to block a user event based on the mail address that has been passed as a parameter.

    Code Snippet

    #The below piece will be global. 
    #Variables
    $ClientID = "xxx"
    $ClientSecret = "xxx"
    $tenantID = "xxx"
    $scope = "https://graph.microsoft.com/.default"
    $authority = "https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token"
    
    $Body = @{
      "grant_type"    = "client_credentials";
      "client_id"     = "$ClientID";
      "client_secret" = "$ClientSecret";
      "scope"      = "$scope";
    }
    
    #Get AccessToken
    $result = Invoke-RestMethod -Method POST -uri $authority -Body $body
    $AccessToken = $result.access_token
    $Accesstoken 
    
    
    $headers = @{
     "Authorization" = "Bearer "+ $AccessToken;
     "Prefer" = 'outlook.timezone="W. Europe Standard Time"';
     "Content-type"  = "application/json"
    }
    
    #making it as a function to invoke it multiple times.
    function update-userevent($mailbox)
    {
    #Create Event
    $mailbox = "[email protected]"
    #Create JSON Object
    $json = @"
    {
      "subject":"Event over Graph API",
      "body": {
        "contentType" : "HTML",
        "content" : "Write Graph API Powershell Script"
      },
      "start": {
          "dateTime" : "2021-07-23T00:00:00",
          "timeZone" : "W. Europe Standard Time"
      },
      "end": {
          "dateTime" : "2021-07-24T00:00:00",
          "timeZone" : "W. Europe Standard Time"
      },
      "location":{
          "displayName" : "HomeOffice"
      }
    }
    "@
    
    $uri = "https://graph.microsoft.com/v1.0/users/$Mailbox/calendar/events"
    Invoke-RestMethod -Method POST -Uri $uri -Headers $headers -Body $json
    
    }
    
    
    #getting the users from Graph...
    #if you are getting the mail address from a predefined list you could ignore the 
    #below 
    $users = Invoke-webRequest 'https://graph.microsoft.com/v1.0/users' -Headers $headers
    #converting the response to JSON
    $json= $users.Content | ConvertFrom-Json
    $mails = $json.value.mail #read from the predefined list
    #iterating through each mailbox
    foreach ($mail in $mails)
    {
    #calling through the above function.
    update-userevent -mailbox $mail
    }