【问题标题】:Powershell COM objectsPowershell COM 对象
【发布时间】:2011-07-25 12:13:36
【问题描述】:

我正在尝试使用以下代码通过 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 直接转换为 Microsoft.Office.Interop.Outlook.Recipient,但这不起作用,我还尝试了 invoke-method() 过程,这里有详细记录:http://www.mcleod.co.uk/scotty/powershell/COMinterop.htm

似乎后者应该可以工作,但它似乎没有为GetSharedDefaultFolder() 所需的多个参数提供规定。

【问题讨论】:

    标签: com powershell outlook


    【解决方案1】:

    我已经设法使用 System.__ComObject 的 InvokeMember 方法来完成这项工作。为了向方法传递多个参数,只需将它们括在括号中即可。

    此处显示了该行代码的示例:

    PS C:> $usercontacts=[System.__ComObject].InvokeMember("GetSharedDefaultFolder" [System.Reflection.BindingFlags]::InvokeMethod,$null,$mapi,($user,10))

    $user 是先前设置的收件人对象。 $mapi 是 MAPI 命名空间对象(之前也设置过)。

    【讨论】:

      【解决方案2】:

      在这里找到解决方案:http://cjoprey.blog.com/2010/03/09/getting-another-users-outlook-folder/

      Add-Type -AssemblyName Microsoft.Office.Interop.Outlook
      
      $class = @”
      using Microsoft.Office.Interop.Outlook;public class MyOL
      {
          public MAPIFolder GetCalendar(string userName)
          {
              Application oOutlook = new Application();
              NameSpace oNs = oOutlook.GetNamespace("MAPI");
              Recipient oRep = oNs.CreateRecipient(userName);
              MAPIFolder calendar = oNs.GetSharedDefaultFolder(oRep, OlDefaultFolders.olFolderCalendar);
              return calendar;
          }
      }
      “@
      
      Add-Type $class -ReferencedAssemblies Microsoft.Office.Interop.Outlook
      

      【讨论】:

      • 与多个 smtp 和 Exchange 帐户一起使用时,Outlook 会怎样?
      【解决方案3】:

      尝试将olFolderCalendar 替换为数字9
      COM 对象需要实际值。它们无法将明文名称转换为常量值。

      【讨论】:

      • 错误处理的是收件人对象,而不是文件夹对象。
      • [Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderCalendar 也应该可以工作
      猜你喜欢
      • 1970-01-01
      • 2013-08-21
      • 2013-01-14
      • 2019-03-02
      • 1970-01-01
      • 2020-09-30
      • 1970-01-01
      • 2019-10-19
      • 2017-06-16
      相关资源
      最近更新 更多