Last active
October 23, 2023 18:38
-
-
Save AlexanderHolmeset/555905e67de9b4d116a67f70336cc842 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
#Enter email account you want to send email reminders from | |
$Email = "enter email here" | |
#Enter Logicapp URL | |
$LogicAppURL = "enter webhook url here" | |
#If subject is hidden in meeting room calendar, set this to true so we get the subject from organizers calendar instead. | |
$HiddenSubject = $true | |
$stopwatch = [System.Diagnostics.Stopwatch]::new() | |
$stopwatch.Start() | |
function AuthenticationFunction { | |
Connect-AzAccount -Identity -ErrorAction Stop | Out-Null | |
$AccessToken = Get-AzAccessToken -ResourceTypeName MSGraph -ErrorAction Stop | select -ExpandProperty Token | |
$global:Header = @{"Authorization" = "Bearer $AccessToken" } | |
$stopwatch = [System.Diagnostics.Stopwatch]::new() | |
$Stopwatch.Start() | |
} | |
AuthenticationFunction | |
#Gets all meetingrooms. | |
$uri = "https://graph.microsoft.com/v1.0/places/microsoft.graph.room?`$top=5000" | |
$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 = @() | |
Foreach($room in $rooms.value){ | |
If($stopwatch.Elapsed.Minutes -gt 40){ | |
AuthenticationFunction | |
} | |
$RoomUPN = $room.emailaddress | |
$RoomDisplayName = $room.displayName | |
$Today = ((Get-Date).Date) | |
$endDate = $Today.AddDays(10) | |
$startDate = get-date $today -Format "yyyy-MM-dd" | |
$endDate = get-date $endDate -Format "yyyy-MM-dd" | |
$startDateinstance = get-date $Today -Format "yyyy-MM-dd" | |
$uri = "https://graph.microsoft.com/v1.0/users/$RoomUPN/calendarview?startDateTime=$($startDate)T00:00:00.0000000&endDateTime=$($endDate)T23:59:00.0000000" | |
$meetings = while (-not [string]::IsNullOrEmpty($uri)) { | |
#API Call | |
$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 | ForEach-Object { | |
$tempDateStart =(Get-Date $($_.Start.datetime)).ToUniversalTime() | |
$tempDateEnd =(Get-Date $($_.end.datetime)).ToUniversalTime() | |
$tzEST = [System.TimeZoneInfo]::FindSystemTimeZoneById("$($_.originalStartTimeZone)") | |
$CurrentTimeStart = [System.TimeZoneInfo]::ConvertTimeFromUtc($tempDateStart, $tzEST) | |
$CurrentTimeEnd = [System.TimeZoneInfo]::ConvertTimeFromUtc($tempDateEnd, $tzEST) | |
$OrganizerDateStart = (Get-Date $($_.Start.datetime) -Format o).split("+")[0] | |
$OrganizerDateStart = get-date $OrganizerDateStart -Format "yyyy-MM-dd" | |
$iCalUId = @() | |
$iCalUId = $_.iCalUId | |
$uri = "https://graph.microsoft.com/v1.0/users/$($_.organizer.emailAddress.address)/calendarview?startDateTime=$($OrganizerDateStart)T00:00:00.0000000&endDateTime=$($OrganizerDateStart)T23:59:00.0000000" | |
$Subject = @() | |
if($HiddenSubject -eq $true){ | |
$Subject = ((Invoke-RestMethod -Uri $uri -Headers $global:Header).Value | Where-Object{$_.iCalUId -like $iCalUId}).Subject | |
$Subject=$Subject.Replace("'","") | |
} | |
else{ | |
$Subject = $_.Subject | |
} | |
#Write-Output $Subject | |
$TempMeetingSeriesList = [PSCustomObject]@{ | |
Subject = $Subject | |
Date = $(Get-Date $CurrentTimeStart -Format "yyyy-MM-dd") | |
StartTime = $(Get-date $CurrentTimeStart -Format "HH:mm") | |
EndTime = $(Get-date $CurrentTimeEnd -Format "HH:mm") | |
ID = $_.ID | |
MeetingRoomUPN = $RoomUPN | |
MeetingRoomDisplayName = $RoomDisplayName | |
OrganizerUPN = $_.organizer.emailAddress.address | |
OrganizerName = $_.organizer.emailAddress.name | |
} | |
$MeetingSeriesList += $TempMeetingSeriesList | |
} | |
} | |
$MeetingSeriesList = $MeetingSeriesList | Group-Object ID| %{ $_.Group | Select 'Subject','Date', 'StartTime', 'EndTime', 'ID', 'MeetingRoomUPN', 'MeetingRoomDisplayName', 'OrganizerUPN', 'OrganizerName' -First 1} | |
$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){ | |
If($stopwatch.Elapsed.Minutes -gt 40){ | |
AuthenticationFunction | |
} | |
$EventsThisWeek = @() | |
$bodyHTML = @() | |
$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>," | |
$bodyHTML+="<table border=1><tr><th>Meeting room</th><th>Booked on</th><th>at Time</th><th>Subject</th><th>Realse Meeting Room</th></tr>" | |
foreach($EventThisWeek in $EventsThisWeek){ | |
$webhookURL = "$LogicAppURL&myString={`"MeetingRoomUPN`": `"$($EventThisWeek.MeetingRoomUPN)`" , `"MeetingID`": `"$($EventThisWeek.ID)`"}" | |
$bodyHTML+="<tr><td>$($EventThisWeek.MeetingRoomDisplayName)</td><td>$($EventThisWeek.Date)</td><td>$($EventThisWeek.StartTime) to $($EventThisWeek.EndTime)</td><td>$($EventThisWeek.Subject)</td><td><a href=\'$webhookURL\'>Click here to realse meeting room</a></td></tr>" | |
}</table> | |
#$bodyHTML+="<p>Thanks,</p><p>MWPS COL Team</p>" | |
$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