我尝试使用以下代码通过 Powershell 从共享日历中获取日历项目:
$outlook = new-object -ComObject Outlook.application
$session = $outlook.Session
$session.Logon("Outlook")
$namespace = $outlook.GetNamespace("MAPI")
$recipient = $namespace.CreateRecipient("John Smith")
$theirCalendar = $namespace.GetSharedDefaultFolder($recipient, "olFolderCalendar")
但我收到类型不匹配错误:
Cannot convert argument "0", with value: "System.__ComObject", for "GetSharedDefaultFolder" to type "Microsoft.Office.I nterop.Outlook.Recipient": "Cannot convert the "System.__ComObject" value of type "System.__ComObject#{00063045-0000-00 00-c000-000000000046}" to type "Microsoft.Office.Interop.Outlook.Recipient"." At line:1 char:34 + $namespace.GetSharedDefaultFolder <<<< ($recipient, "olFolderCalendar") + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
我尝试直接将 $recipient 转换为 Microsoft.Office.Interop.Outlook.Recipient
,但这不起作用,而且我还尝试了 invoke-method()
程序在这里有详细记录:http://www.mcleod.co.uk/scotty/powershell/COMinterop.htm
看起来后者应该可以工作,但它似乎没有提供 GetSharedDefaultFolder()
所需的多个参数。
最佳答案
我已经设法使用 System.__ComObject 的 InvokeMember 方法来实现此功能。为了将多个参数传递给该方法,只需将它们括在括号中即可。
此处显示了该代码行的示例:
PS C:> $usercontacts=[System.__ComObject].InvokeMember("GetSharedDefaultFolder"[System.Reflection.BindingFlags]::InvokeMethod,$null,$mapi,($user,10))
$user是之前设置的接收者对象。 $mapi 是 MAPI 命名空间对象(也是之前设置的)。
关于com - Powershell COM 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5476786/