com - Powershell COM 对象

标签 com powershell outlook

我尝试使用以下代码通过 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/

相关文章:

c++ - COM 服务器的奇怪行为

vba - 需要骨架代码从 PythonWin 调用 Excel VBA

powershell - Windows 下的 Unix tail 和 grep 等效项

vba - Outlook VbaProject.OTM 时间戳在更改时不会更新

c# - 从 Outlook 加载项调用时,NLog 不写入日志文件

c# - stdole.dll 有什么作用?

windows - COM 如何选择如何编码接口(interface)?

powershell - 如果我在服务器或工作站上,如何从Powershell中查找?

powershell - Powershell 2.0文件外格式化的噩梦

vba - 引用不在默认收件箱下的文件夹