-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-MyNextMeeting.ps1
139 lines (116 loc) · 3.42 KB
/
Get-MyNextMeeting.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
<#
.SYNOPSIS
Gets the next calendar meeting from Exchange Online.
.DESCRIPTION
Get-MyNextMeeting retrieves and displays details about the next scheduled meeting
from Exchange Online within a specified number of days. Shows meeting subject,
organizer, location, start/end times, and duration in a human-readable format.
.PARAMETER Days
Number of days to look ahead for meetings. Defaults to 7 days.
.EXAMPLE
.\Get-MyNextMeeting.ps1
Shows the next meeting within the default 7 day window.
.EXAMPLE
.\Get-MyNextMeeting.ps1 -Days 14
Shows the next meeting within the next 14 days.
.NOTES
File Name : Get-MyNextMeeting.ps1
Author : Jonathan Cormier
Prerequisite : ExchangeOnlineManagement Module
Created : December 5, 2024
#>
[CmdletBinding()]
param(
[Parameter(
HelpMessage = "Number of days to look ahead for meetings"
)]
[ValidateRange(1, 365)]
[int]$Days = 7
)
function Test-ExchangeOnlineConnection {
[CmdletBinding()]
param()
try {
# Check if module is installed
if (!(Get-Module -ListAvailable -Name ExchangeOnlineManagement)) {
return $false
}
# Check if module is imported in current session
if (!(Get-Module ExchangeOnlineManagement)) {
return $false
}
# Check connection status
if (!((Get-ConnectionInformation).State -eq 'Connected')) {
return $false
}
return $true
}
catch {
return $false
}
}
function Get-TimeUntil {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[DateTime]$FutureTime
)
$timeUntil = $FutureTime - (Get-Date)
if ($timeUntil.Days -gt 0) {
return "$($timeUntil.Days) days and $($timeUntil.Hours) hours"
}
elseif ($timeUntil.Hours -gt 0) {
return "$($timeUntil.Hours) hours and $($timeUntil.Minutes) minutes"
}
else {
return "$($timeUntil.Minutes) minutes"
}
}
function Get-HumanDuration {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[TimeSpan]$Duration
)
if ($Duration.Hours -gt 0) {
if ($Duration.Minutes -gt 0) {
return "$($Duration.Hours) hour$(if($Duration.Hours -ne 1){'s'}) and $($Duration.Minutes) minute$(if($Duration.Minutes -ne 1){'s'})"
}
return "$($Duration.Hours) hour$(if($Duration.Hours -ne 1){'s'})"
}
return "$($Duration.Minutes) minute$(if($Duration.Minutes -ne 1){'s'})"
}
if (!(Test-ExchangeOnlineConnection)) {
# Handle the disconnected state
Write-Error "Exchange Online connection required"
exit
}
$startDate = Get-Date
$endDate = $startDate.AddDays($Days)
try {
$nextMeeting = Get-CalendarView -StartDate $startDate -EndDate $endDate |
Select-Object -First 1 -Property @(
'Subject',
'Start',
'End',
'Duration',
'Organizer',
'Location'
)
# Output next hellscape
if ($nextMeeting) {
$beginsText = Get-TimeUntil -FutureTime $nextMeeting.Start
$durationText = Get-HumanDuration -Duration $nextMeeting.Duration
Write-Host "`nSubject: $($nextMeeting.Subject)"
Write-Host "Organizer: $($nextMeeting.Organizer)"
Write-Host "Location: $($nextMeeting.Location)"
Write-Host "Begins: $beginsText"
Write-Host "Duration: $durationText"
Write-Host "Start: $($nextMeeting.Start.ToString('R'))"
Write-Host "End: $($nextMeeting.End.ToString('R'))"
} else {
Write-Host "`nNo scheduled meetings found" -ForegroundColor Green
}
} catch {
Write-Error "Failed to retrieve calendar: $_"
}