This file contains 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
Connect-AzAccount -Identity -ErrorAction Stop | Out-Null | |
$AccessToken = Get-AzAccessToken -ResourceTypeName MSGraph -ErrorAction Stop | select -ExpandProperty Token | |
$global:Header = @{"Authorization" = "Bearer $AccessToken" } | |
#Enter email account you want to send email reminders from | |
$Email = "alexander@contoso.com" | |
$LogicAppURL = "Enter webhok URL here." | |
#Gets all meetingrooms. | |
$uri = "https://graph.microsoft.com/v1.0/places/microsoft.graph.room" | |
$Rooms = while (-not [string]::IsNullOrEmpty($uri)) { | |
# API Call | |
# Write-Host "`r`nQuerying $currentUri..." -ForegroundColor Yellow | |
$apiCall = Invoke-WebRequest -Method "GET" -Uri $uri -ContentType "application/json" -Headers $global:Header -ErrorAction Stop -UseBasicParsing | |
$nextLink = $null | |
$uri = $null | |
if ($apiCall.Content) { | |
# Check if any data is left | |
$nextLink = $apiCall.Content | ConvertFrom-Json | Select-Object '@odata.nextLink' | |
$uri = $nextLink.'@odata.nextLink' | |
$apiCall.Content | ConvertFrom-Json | |
} | |
} | |
$MeetingSeriesList = @() | |
#Looks through all meetingrooms for meeting series that started 365 days ago, then find all instances of that meeting series. | |
foreach($room in $rooms.value){ | |
$RoomUPN = $room.emailaddress | |
$RoomDisplayName = $room.displayName | |
$Today = ((Get-Date).Date) | |
$startDate = $Today.AddDays(-365) | |
$endDate = $Today.AddDays(10) | |
$startDate = get-date $startDate -Format "yyyy-MM-dd" | |
$uri = "https://graph.microsoft.com/v1.0/users/$RoomUPN/calendar/events?`$filter=Start/DateTime ge '$($startdate)T00:00:00Z'" | |
$meetings = while (-not [string]::IsNullOrEmpty($uri)) { | |
# API Call | |
# Write-Host "`r`nQuerying $currentUri..." -ForegroundColor Yellow | |
$apiCall = Invoke-WebRequest -Method "GET" -Uri $uri -ContentType "application/json" -Headers $global:Header -ErrorAction Stop -UseBasicParsing | |
$nextLink = $null | |
$uri = $null | |
if ($apiCall.Content) { | |
# Check if any data is left | |
$nextLink = $apiCall.Content | ConvertFrom-Json | Select-Object '@odata.nextLink' | |
$uri = $nextLink.'@odata.nextLink' | |
$apiCall.Content | ConvertFrom-Json | |
} | |
} | |
$meetings.value | Where-Object { $_.type -eq "seriesMaster"} | ForEach-Object { | |
$seriesMaster = $_ | |
If($($_.recurrence.range.enddate) -like "0001-01-01"){ | |
$Instanceenddate = get-date $endDate -Format "yyyy-MM-dd" | |
} | |
else{$Instanceenddate = $($_.recurrence.range.enddate)} | |
$uri = "https://graph.microsoft.com/v1.0/users/$RoomUPN/events/\$($_.id)/instances?startDateTime=$($_.recurrence.range.startdate)&endDateTime=$Instanceenddate" | |
$instances = Invoke-RestMethod -Uri $uri -Headers $global:Header | |
foreach($instance in $instances.value){ | |
$TempMeetingSeriesList = [PSCustomObject]@{ | |
Subject = $instance.subject | |
Date = $(Get-Date ($instance.start).datetime -Format "yyyy-MM-dd") | |
ID = $instance.ID | |
MeetingRoomUPN = $RoomUPN | |
MeetingRoomDisplayName = $RoomDisplayName | |
OrganizerUPN = $instance.organizer.emailAddress.address | |
OrganizerName = $instance.organizer.emailAddress.name | |
} | |
$MeetingSeriesList += $TempMeetingSeriesList | |
} | |
} | |
} | |
$Today = (Get-Date).date | |
$Sunday = Get-Date ($Today.AddDays(1)) -Format yyyy-MM-dd | |
$NextSunday = Get-Date ($Today.AddDays(8)) -Format yyyy-MM-dd | |
#Finds all unique meeting organizers. | |
$users = $MeetingSeriesList | Select-Object OrganizerUPN,OrganizerName -Unique | |
#Find all the meetingrooms a organizer have booked this week, and send email with reminder to see if any rooms can be canceled. | |
foreach($user in $users){ | |
$EventsThisWeek = @() | |
$EventsThisWeek = $MeetingSeriesList | Where-Object{$_.date -ge $sunday -and $_.date -lt $NextSunday -and $_.organizerupn -like $user.organizerupn} | |
If($EventsThisWeek){ | |
$EventsThisWeek | ft | |
$Title="Here are your meeting room bookings for this week." | |
$bodyHTML ='<doctype html><html><head><title>' + $Title +' </title></head><body><font face="Calibri" size="3">' | |
$bodyHTML+="<p>Hi $($user.OrganizerName)!</p><p>Please cancel any meetingrooms you wont use this week<b><i></i></b>," | |
foreach($EventThisWeek in $EventsThisWeek){ | |
$webhookURL = "$LogicAppURL&myString={`"MeetingRoomUPN`": `"$($EventThisWeek.MeetingRoomUPN)`" , `"MeetingID`": `"$($EventThisWeek.ID)`"}" | |
$bodyHTML+=" <ul><li>Click here to cancel room $($EventThisWeek.MeetingRoomDisplayName) with subject $($EventThisWeek.Subject) for $($EventThisWeek.Date): <a href=\'$webhookURL\'>$($EventThisWeek.MeetingRoomDisplayName)</a> <br />" | |
} | |
$BodyEmail = @" | |
{ | |
"message": { | |
"subject": "$Title", | |
"body": { | |
"contentType": "HTML", | |
"content": '$bodyHTML' | |
}, | |
"toRecipients": [ | |
{ | |
"emailAddress": { | |
"address": "$($user.OrganizerUPN)" | |
} | |
} | |
] | |
}, | |
"saveToSentItems": "false" | |
} | |
"@ | |
Invoke-RestMethod -Method POST -Uri "https://graph.microsoft.com/v1.0/users/$email/sendMail" -Headers $global:Header -body $BodyEmail -ContentType "application/json;charset=utf-8" | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment