1
+ <#
2
+ . SYNOPSIS
3
+ Converts a WebUntis timetable to an ICS calendar file.
4
+
5
+ . DESCRIPTION
6
+ This script retrieves timetable data from the WebUntis API and converts it into an ICS calendar file format.
7
+ It allows specifying a date range for the timetable data.
8
+
9
+ . PARAMETER baseUrl
10
+ The base URL of the WebUntis API.
11
+
12
+ . PARAMETER elementType
13
+ The type of element to filter by (default is 1, this should return a timetable).
14
+
15
+ . PARAMETER elementId
16
+ The classes timetable ID.
17
+
18
+ . PARAMETER dates
19
+ An array of dates (either as strings or DateTime objects) for which to retrieve timetable data.
20
+ The default is the current week and the next three weeks.
21
+
22
+ . PARAMETER OutputFilePath
23
+ The file path where the ICS file will be saved. The default is "calendar.ics".
24
+
25
+ . PARAMETER cookie
26
+ The cookie value for the WebUntis session.
27
+
28
+ . PARAMETER tenantId
29
+ The tenant ID for the WebUntis session.
30
+
31
+ . EXAMPLE
32
+ .\timeTableToIcs.ps1 -baseUrl "your.webuntis.url" -elementType 1 -elementId 12345 -dates "2023-01-01", "2023-01-08" -OutputFilePath "mycalendar.ics" -cookie "your_cookie" -tenantId "your_tenant_id"
33
+
34
+ . NOTES
35
+ Author: Markus Noack
36
+ Date: 2024-09-28
37
+ Version: 1.0
38
+ #>
39
+
1
40
param (
41
+ [ValidateNotNullOrEmpty ()]
42
+ [Alias (" URL" )]
2
43
[string ]$baseUrl ,
3
44
[int ]$elementType = 1 ,
45
+ [Alias (" TimeTableID" )]
4
46
[int ]$elementId ,
5
47
[Parameter (Mandatory = $false )]
6
48
[Alias (" Date" )]
@@ -15,16 +57,24 @@ param (
15
57
}
16
58
$true
17
59
})]
18
- [System.Object []]$dates = @ ( (-7 .. 14 | ForEach-Object { (Get-Date ).AddDays($_ ) })[0 , 7 , 14 ] ),
60
+ [System.Object []]$dates = @ ( (@ (-7 , 0 , 7 , 14 ) | ForEach-Object { (Get-Date ).AddDays($_ ) }) ),
61
+ [ValidateNotNullOrEmpty ()]
19
62
[string ]$OutputFilePath = " calendar.ics" ,
63
+ [ValidateNotNullOrEmpty ()]
20
64
[string ]$cookie ,
65
+ [ValidateNotNullOrEmpty ()]
21
66
[string ]$tenantId
22
67
)
23
68
69
+ $datesCount = 0
24
70
# Convert any string inputs to DateTime objects
25
71
$dates = $dates | ForEach-Object {
72
+ $datescount ++
26
73
if ($_ -is [string ]) { [datetime ]::Parse($_ ) } else { $_ }
27
74
}
75
+ if ($datesCount -gt 4 ) {
76
+ throw " The maximum number of weeks is 4. (Limit by WebUntis API)"
77
+ }
28
78
29
79
function Get-SingleElement {
30
80
param (
@@ -80,7 +130,6 @@ $session.Cookies.Add((New-Object System.Net.Cookie("traceId", "9de4710537aa09759
80
130
$session.Cookies.Add ((New-Object System.Net.Cookie(" JSESSIONID" , " B9ED9B2D36BE7D25A7A9EF21E8144D3F" , " /" , " $baseUrl " )))
81
131
82
132
$periods = [System.Collections.Generic.List [PeriodEntry ]]::new()
83
-
84
133
$courses = [System.Collections.Generic.List [Course ]]::new()
85
134
$rooms = [System.Collections.Generic.List [Room ]]::new()
86
135
@@ -178,6 +227,37 @@ $IcsEntries = [System.Collections.Generic.List[string]]::new()
178
227
foreach ($icsEvent in $calendarEntries ) {
179
228
$IcsEntries += $icsEvent.ToIcsEntry ()
180
229
}
230
+
231
+
232
+ # Create the .ics file content
233
+ $icsContent = @"
234
+ BEGIN:VCALENDAR
235
+ VERSION:2.0
236
+ PRODID:-//Chaos_02//WebUntisToIcs//EN
237
+ X-WR-CALNAME:$ ( $class.displayname )
238
+ $ ( ($IcsEntries -join " `n " ))
239
+ END:VCALENDAR
240
+ "@
241
+
242
+ try {
243
+ if ($OutputFilePath ) {
244
+ # Write the .ics content to a file
245
+ Set-Content - Path $OutputFilePath - Value $icsContent
246
+ Write-Output " ICS file created at $ ( (Get-Item - Path $OutputFilePath ).FullName) "
247
+ }
248
+ else {
249
+ # Write the .ics content to a variable
250
+ $icsVariable = $icsContent
251
+ Write-Output $icsVariable
252
+ return $icsVariable
253
+ }
254
+ }
255
+ catch {
256
+ Write-Error " An error occurred while creating the ICS file: $_ "
257
+ throw
258
+ }
259
+
260
+ # ###### Class definitions #######
181
261
182
262
class IcsEvent {
183
263
[string ]$StartTime
@@ -224,36 +304,6 @@ END:VEVENT
224
304
}
225
305
}
226
306
227
-
228
-
229
- # Create the .ics file content
230
- $icsContent = @"
231
- BEGIN:VCALENDAR
232
- VERSION:2.0
233
- PRODID:-//Chaos_02//WebUntisToIcs//EN
234
- X-WR-CALNAME:$ ( $class.displayname )
235
- $ ( ($IcsEntries -join " `n " ))
236
- END:VCALENDAR
237
- "@
238
-
239
- try {
240
- if ($OutputFilePath ) {
241
- # Write the .ics content to a file
242
- Set-Content - Path $OutputFilePath - Value $icsContent
243
- Write-Output " ICS file created at $ ( (Get-Item - Path $OutputFilePath ).FullName) "
244
- }
245
- else {
246
- # Write the .ics content to a variable
247
- $icsVariable = $icsContent
248
- Write-Output $icsVariable
249
- return $icsVariable
250
- }
251
- }
252
- catch {
253
- Write-Error " An error occurred while creating the ICS file: $_ "
254
- throw
255
- }
256
-
257
307
class rescheduleInfo {
258
308
[datetime ]$startTime
259
309
[datetime ]$endTime
0 commit comments