Powershell COM 对象
我尝试使用以下代码通过 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")
但出现类型不匹配错误:
无法将参数“0”(值为“System.__ComObject”)转换为“GetSharedDefaultFolder”以键入“Microsoft.Office.I” nterop.Outlook.Recipient": "无法转换类型为“System.__ComObject#{00063045-0000-00”的“System.__ComObject”值 00-c000-000000000046}”键入“Microsoft.Office.Interop.Outlook.Recipient”。 行:1 字符:34 + $namespace.GetSharedDefaultFolder <<<<< ($recipient,“olFolderCalendar”) + 类别信息:未指定:(:) [],MethodException + FullQualifiedErrorId:MethodArgumentConversionInvalidCastArgument
我尝试直接将 $recipient 转换为 Microsoft.Office.Interop.Outlook.Recipient
,但这不起作用,而且我还尝试了 invoke-method( )
过程在这里有详细记录:http://www.mcleod。 co.uk/scotty/powershell/COMinterop.htm
看起来后者应该可以工作,但它似乎没有提供 GetSharedDefaultFolder()
所需的多个参数。
I am attempting to get calendar items from a shared calendar via Powershell with the following code:
$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")
but I am getting a type mismatch error:
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
I've tried directly casting $recipient to a Microsoft.Office.Interop.Outlook.Recipient
, which doesn't work, and I have also tried the invoke-method()
procedure well documented here: http://www.mcleod.co.uk/scotty/powershell/COMinterop.htm
It seems like the latter should work, but it doesn't appear to have provisions for the multiple parameters that GetSharedDefaultFolder()
requires.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已经设法使用 System.__ComObject 的 InvokeMember 方法来实现此功能。为了将多个参数传递给该方法,只需将它们括在括号中即可。
该代码行的示例如下所示:
PS C:> $usercontacts=[System.__ComObject].InvokeMember("GetSharedDefaultFolder" [System.Reflection.BindingFlags]::InvokeMethod,$null,$mapi,($user,10))
$user 是之前设置的收件人对象。
$mapi 是 MAPI 命名空间对象(也是之前设置的)。
I have managed to get this working using the InvokeMember method of System.__ComObject. In order to pass multiple parameters to the method, simply enclose them in parentheses.
An example of the line of code is shown here:
PS C:> $usercontacts=[System.__ComObject].InvokeMember("GetSharedDefaultFolder" [System.Reflection.BindingFlags]::InvokeMethod,$null,$mapi,($user,10))
$user is the recipient object previously set up.
$mapi is the MAPI namespace object (also set up previously).
在这里找到了解决方案: http://cjoprey.blog .com/2010/03/09/getting-another-users-outlook-folder/
Found a solution here: http://cjoprey.blog.com/2010/03/09/getting-another-users-outlook-folder/
尝试将
olFolderCalendar
替换为数字9
。COM 对象需要实际值。它们无法将明文名称转换为常量值。
Try replacing
olFolderCalendar
with the number9
.COM objects need the actual values. They cannot convert clear text names to constant values.