Search code examples
powershellpackage-management

read an ics file using iCal.Net and PowerShell


I am trying to read an .ics file using powershell and the iCal.Net package.

# Install-Package NodaTime -Force -MaximumVersion 3.0.0
# Install-Package iCal.Net -Force

# load assemblies
Write-Host loading assemblies -ForegroundColor Green
Add-Type -LiteralPath 'C:\Program Files\PackageManagement\NuGet\Packages\NodaTime.3.0.0\lib\netstandard2.0\NodaTime.dll'
Add-Type -LiteralPath 'C:\Program Files\PackageManagement\NuGet\Packages\Ical.Net.4.2.0\lib\netstandard2.0\Ical.Net.dll'

# define ics url
Write-Host defining ics url -ForegroundColor Green
$icsurl = 'https://url_to/basic.ics'

# load ics
Write-Host invoking web request -ForegroundColor Green
$response = Invoke-WebRequest $icsurl

# deserialize ICS
Write-Host deserializing ics -ForegroundColor Green
$calendar = [Ical.Net.Calendar]::Load( $response.Content )

Write-Host $calendar

Even though I can see the .ics file contains multiple events, the $calendar variable doesn't return any results.

This is the output of the script

loading assemblies
defining ics url
invoking web request
deserializing ics
Ical.Net.Calendar

And $response.Content simply shows me the contents of the .ics file


Solution

  • From this example it looks like you have to use the static method Calendar.Load(). It accepts a string, so you don't need to download to a file. You can do everything in memory.

    A static method is called without an object, instead you call it using :: on a class.

    # download ICS
    $response = Invoke-WebRequest $icsurl
    
    # TODO: handle download errors
    
    # deserialize ICS
    $calendar = [Ical.Net.Calendar]::Load( $response.Content )
    

    To discover static members you can use this command:

    [Ical.Net.Calendar] | Get-Member -Static