-
Notifications
You must be signed in to change notification settings - Fork 0
/
too-many-meetings.ps1
338 lines (296 loc) · 11.2 KB
/
too-many-meetings.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/pwsh
<#
MIT License
Copyright (c) 2022 LGDan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
#>
function Set-MSGraphAPIToken {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSUseDeclaredVarsMoreThanAssignments',
'',
Justification='MS Graph token is used outside function scope.'
)]
[CmdletBinding()]
param (
[Parameter()][String]$Token
)
$Global:MSGraphToken = $Token
}
function Invoke-MSGraph {
[CmdletBinding()]
param (
[Parameter()][String]$URL,
[Parameter()][String]$Method,
[Parameter()]$Body
)
if ($null -eq $Global:MSGraphToken) {
"Use Set-MSGraphAPIToken -Token <Token>"
}else{
Invoke-RestMethod -Uri $URL -Method $Method -Body $Body -Headers @{
Authorization = "Bearer $Global:MSGraphToken"
}
}
}
function Build-URL {
[CmdletBinding()]
param (
[Parameter()][String]$Protocol,
[Parameter()][String]$Domain,
[Parameter()][String]$Path,
[Parameter()][hashtable]$GetParameters
)
$sb = [System.Text.StringBuilder]::new()
$sb.Append($Protocol + "://") | Out-Null
$sb.Append($Domain) | Out-Null
$sb.Append($Path) | Out-Null
if ($null -ne $GetParameters) {$sb.Append("?")|Out-Null}
$GetParameters.GetEnumerator() | ForEach-Object {
$sb.Append(($_.Key + "=" + $_.Value)) | Out-Null
$sb.Append("&") | Out-Null
}
$sb.Remove(($sb.Length-1),1) | Out-Null
$sb.ToString()
}
function Get-ThisWeek {
$monday = (Get-Date).Subtract(
# Always gets monday, but with current time.
[Timespan]::FromDays(
(Get-Date).DayOfWeek.value__-1
)
).Subtract(
# Negate current time to get 00:00:00
(Get-Date).TimeOfDay.Subtract([timespan]::FromSeconds(1))
)
$friday = $monday.AddDays(5)
$friday = $friday.Subtract([timespan]::FromSeconds(1))
[ordered]@{
StartDate = $monday
EndDate = $friday
}
}
function Get-CalendarDaysIntoTheFuture {
[CmdletBinding()]
param (
[Parameter()][Int]$DaysIntoTheFuture
)
Get-Calendar `
-StartDate (Get-Date) `
-EndDate (Get-Date).AddDays($DaysIntoTheFuture)
}
function Get-Calendar {
[CmdletBinding()]
param (
[Parameter()][datetime]$StartDate,
[Parameter()][datetime]$EndDate
)
# https://graph.microsoft.com
# /v1.0/me/calendarview
# ?startdatetime=2022-01-08T15:56:22.840Z
# &enddatetime=2022-01-15T15:56:22.840Z
$url = Build-URL `
-Protocol "https" `
-Domain "graph.microsoft.com" `
-Path "/v1.0/me/calendarview" `
-GetParameters ([ordered]@{
startdatetime = $StartDate.ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
enddatetime = $EndDate.ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
})
$allData = [System.Collections.ArrayList]::new()
$calendarData = Invoke-MSGraph -URL $url -Method "GET"
$allData.Add($calendarData) | Out-Null
if ($null -ne $calendarData."@odata.nextLink") {
Do {
$calendarData = Invoke-MSGraph `
-URL $calendarData."@odata.nextLink" `
-Method "GET"
$allData.Add($calendarData) | Out-Null
} While ($null -ne $calendarData."@odata.nextLink")
}
$allData | ForEach-Object {
$_.value
}
}
# Why are these two functions below the same operation, but named differently?
# Because the same operation is used for two different reasons, and it
# increases readability for those who aren't familiar with the code.
function Get-MinutesOfTheWorkingDay($StartTime, $EndTime) {
([datetime]::Parse($EndTime) - [datetime]::Parse($StartTime)).TotalMinutes
}
function Get-MinuteBasedOnStart($StartTime, $InputTime) {
([datetime]::Parse($InputTime) - [datetime]::Parse($StartTime)).TotalMinutes
}
function Get-MeetingEvents {
[CmdletBinding()]
param (
[Parameter()]
[Int]
$DaysIntoTheFuture
)
<#
This is just a filter query to avoid any calendar appointments that
aren't disruptive / don't count toward suffering (i.e. lunch).
Add in any more filters here to exclude any other events.
Full list of stuff to filter by:
https://docs.microsoft.com/en-us/graph/api/resources/event?view=graph-rest-1.0
#>
$week = Get-ThisWeek
Get-Calendar -StartDate $week.StartDate -EndDate $week.EndDate
| Where-Object categories -NotContains "No Conflicts" # Unique to my cal.
| Select-Object start, end, subject, responseStatus
| ForEach-Object {
[pscustomobject]@{
date = ([System.DateTime]$_.start.dateTime).ToShortDateString()
event = $_
}
}
}
function Get-HowMuchTimeCanIActuallyDoWork {
[CmdletBinding()]
param (
[Parameter()][Hashtable]$WorkingHoursTable,
[Parameter()][timespan]$BreaktimeDuration
)
# Get all the calendar events up and coming, and sort them by the day.
Get-MeetingEvents
| Group-Object date
| ForEach-Object {
# For each day...
$events = $_.Group
$date = $_.Name
# Get the facts about the day, and preload a list of consumable minutes.
$dayText = [datetime]::Parse($date).ToString("dddd")
$dayStart = $WorkingHoursTable[$dayText][0]
$dayEnd = $WorkingHoursTable[$dayText][1]
$breakTimeMinutes = $BreaktimeDuration.TotalMinutes
$totalMinutesToday = Get-MinutesOfTheWorkingDay `
-StartTime $dayStart `
-EndTime $dayEnd
$minutesList = [System.Collections.Generic.list[Int]]::new()
1..$totalMinutesToday | ForEach-Object {$minutesList.Add($_)|Out-Null}
# Remove 'used' minutes from the list of available minutes in the
# working day. This means that double-booked time does not matter.
foreach ($event in $events) {
$meetingStartMinutes = Get-MinuteBasedOnStart `
-StartTime $dayStart `
-InputTime $event.event.start.dateTime.ToString("HH:mm")
$meetingDuration = (
$event.event.end.dateTime - $event.event.start.dateTime
)
$meetingEndMinutes = (
$meetingStartMinutes + $meetingDuration.TotalMinutes
)
$meetingStartMinutes..($meetingEndMinutes-1) | ForEach-Object {
$minutesList.Remove($_) | Out-Null
}
}
# Now all consumed minutes have been removed from the list of available
# minutes, lets add up what's left over to find out how many 'free'
# miuntes there are left to do work.
$availableTime = [timespan]::FromMinutes(
($minutesList.Count - ($breakTimeMinutes))
)
$unavailableTime = [timespan]::FromMinutes(
$totalMinutesToday - $minutesList.Count
)
# Finally spit out an object containing all the stats for this day.
[PSCustomObject]@{
Date = $date
TotalTimeInMeetings = $unavailableTime
TotalTimeFree = $availableTime
EventCount = ($events | Measure-Object).Count
PercentInMeetings = (
$unavailableTime.TotalMinutes / $totalMinutesToday
).ToString("P") # Percentage formatting w/auto dec place?! Who knew!
PercentFree = (
$availableTime.TotalMinutes / $totalMinutesToday
).ToString("P")
}
}
}
function Get-ThePercentageOfMyWeekSpentInMeetings {
# Works out the percentage of your working week that you spend in meetings.
[CmdletBinding()]
param (
[Parameter()][Int]$WorkingHoursPerWeek,
[Parameter()][Hashtable]$WorkingHoursTable,
[Parameter()][timespan]$BreaktimeDuration
)
$totalTimeInMeetings = [timespan]::Zero
$totalTimeFree = [timespan]::Zero
$workingHours = [timespan]::FromHours($WorkingHoursPerWeek)
Get-HowMuchTimeCanIActuallyDoWork `
-WorkingHoursTable $WorkingHoursTable `
-BreaktimeDuration $BreaktimeDuration
| ForEach-Object {
$totalTimeInMeetings = $totalTimeInMeetings.Add($_.TotalTimeInMeetings)
$totalTimeFree = $totalTimeFree.Add($_.TotalTimeFree)
}
[PSCustomObject]@{
TimeInMeetings = $totalTimeInMeetings
TimeFree = $totalTimeFree
PercentageInMeetings = (
$totalTimeInMeetings.TotalMinutes / $workingHours.TotalMinutes
).ToString("P")
PercentageFree = (
$totalTimeFree.TotalMinutes / $workingHours.TotalMinutes
).ToString("P")
}
}
function Get-TotalWorkingHours {
[CmdletBinding()]
param (
[Parameter()][Hashtable]$WorkingHoursTable,
[Parameter()][timespan]$BreaktimeDuration
)
$totalHours = [timespan]::Zero
$WorkingHoursTable.GetEnumerator() | ForEach-Object {
$timeWorkedToday = (
[datetime]::Parse($_.Value[1]) - [datetime]::Parse($_.Value[0])
).Subtract($BreaktimeDuration)
$totalHours = $totalHours.Add($timeWorkedToday)
}
$totalHours
}
function Invoke-Pain {
if ($null -ne $Global:MSGraphToken) {
$myWorkingHours = @{
Monday = ("09:00","17:30")
Tuesday = ("09:00","17:30")
Wednesday = ("09:00","17:30")
Thursday = ("09:00","17:00")
Friday = ("09:00","16:30")
}
$myBreakTime = [timespan]::FromHours(1)
[timespan]$myWorkingHoursTotal = Get-TotalWorkingHours `
-WorkingHoursTable $myWorkingHours `
-BreaktimeDuration $myBreakTime
Write-Output "------ Days of this week in meetings ----"
Get-HowMuchTimeCanIActuallyDoWork `
-WorkingHoursTable $myWorkingHours `
-BreaktimeDuration $myBreakTime
Write-Output "-----------------------------------------"
Write-Output "------ Review of this week --------------"
Get-ThePercentageOfMyWeekSpentInMeetings `
-WorkingHoursPerWeek $myWorkingHoursTotal.TotalHours `
-WorkingHoursTable $myWorkingHours `
-BreaktimeDuration $myBreakTime
Write-Output "-----------------------------------------"
}else{
Write-Host "Set your token with Set-MSGraphAPIToken -Token ""tokenfoo"""
}
}
Invoke-Pain