【问题标题】:read an ics file using iCal.Net and PowerShell使用 iCal.Net 和 PowerShell 读取 ics 文件
【发布时间】:2023-02-25 21:40:11
【问题描述】:

我正在尝试使用 powershell 和 iCal.Net 包读取 .ics 文件。

# install prerequisites
# Install-Package NodaTime -Force
# Install-Package iCal.Net -Force

# load assemblies
Write-Host loading assemblies -ForegroundColor Green
Add-Type -LiteralPath (Join-Path (Split-Path -Parent (Get-Package ICal.Net).Source) lib\netstandard2.0\Ical.Net.dll)
Add-Type -LiteralPath (Join-Path (Split-Path -Parent (Get-Package NodaTime).Source) lib\netstandard2.0\NodaTime.dll)
Add-Type -LiteralPath (Join-Path (Split-Path -Parent (Get-Package NodaTime).Source) lib\net6.0\NodaTime.dll)

# set variables
Write-Host defining ics url -ForegroundColor Green
$icsurl = "https://url_to/basic.ics"

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

# create calendar object
Write-Host creating calendar object -ForegroundColor Green
$calendar = New-Object Ical.Net.Calendar

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

尝试加载 .ics 文件时,无论我选择哪个选项,我都会收到错误消息

这是错误

Method invocation failed because [Ical.Net.Calendar] does not contain a method named 'Calendar'.
At line:1 char:1
+ $calendar.Calendar($icsfile)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

有什么办法可以获得此软件包功能的列表吗?我查看了 Get-Command 选项,但它适用于模块,这是一个包。

我的目标是在 .ics 文件中生成一个可用事件表,并显示开始日期、开始时间和摘要。

根据 cmets 更新代码以显示加载 NodaTime ddl 和反序列化 ics 文件的命令。新的错误是:

Exception calling "Load" with "1" argument(s): "Could not load file or assembly 'NodaTime, Version=3.0.0.0, Culture=neutral, PublicKeyToken=4226afe0d9b296d1' or one of its dependencies. The        
system cannot find the file specified."
At C:\Users\Windows\Desktop\repo\test.ps1:26 char:1
+ $calendar = [Ical.Net.Calendar]::Load( $response.Content )
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FileNotFoundException

【问题讨论】:

  • 日历和属性还是一种方法?错误说“方法”。

标签: powershell package-management


【解决方案1】:

来自this example 看起来你必须使用静止的方法Calendar.Load()。它接受一个字符串,所以你不需要下载到一个文件。你可以在记忆中做任何事情。

static method 是在没有对象的情况下调用的,而是使用 ::班级.

# download ICS
$response = Invoke-WebRequest $icsurl

# TODO: handle download errors

# deserialize ICS
$calendar = [Ical.Net.Calendar]::Load( $response.Content )

要发现静态成员,您可以使用此命令:

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

【讨论】:

  • 在“反序列化 ICS”命令上我收到一个错误:使用“1”参数调用“加载”的异常:“无法加载文件或程序集‘NodaTime,Version=3.0.0.0,Culture=neutral,PublicKeyToken=4226afe0d9b296d1’或其依赖项之一。系统找不到指定的文件。”即使我已经加载了 NodaTime dll。
  • @aristosv 试试这个:Install-Package NodaTime -Force -MaximumVersion 3.0.0
  • @aristosv 同样,在Add-Type行中使用Get-Package NodaTime -MaximumVersion 3.0.0
猜你喜欢
  • 2017-05-09
  • 1970-01-01
  • 1970-01-01
  • 2016-05-19
  • 1970-01-01
  • 2014-01-18
  • 1970-01-01
  • 2018-01-10
  • 2018-08-29
相关资源
最近更新 更多