Powershell COM对象

Powershell COM对象,com,powershell,outlook,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") $theirCalend

我正在尝试通过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")
但我得到一个类型不匹配错误:

无法将“GetSharedDefaultFolder”的值为“System.\u ComObject”的参数“0”转换为类型“Microsoft.Office.I” interop.Outlook.Recipient“:”无法转换“系统”类型的“系统”值 00-c000-0000000000 46}”输入“Microsoft.Office.Interop.Outlook.Recipient” 第1行字符:34
+$namespace.GetSharedDefaultFolder尝试将
olFolderCalendar
替换为编号
9

COM对象需要实际值。它们无法将明文名称转换为常量值。

在此处找到了解决方案:


我使用System的InvokeMember方法成功地实现了这一点。为了向方法传递多个参数,只需将它们括在括号中即可

代码行的示例如下所示:

PS C:>$usercontacts=[System.\u ComObject].InvokeMember(“GetSharedDefaultFolder”[System.Reflection.BindingFlags]::InvokeMethod,$null,$mapi,($user,10))

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

此错误处理的是收件人对象,而不是文件夹对象。[Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderCalendar也可以工作Outlook与多个smtp和Exchange帐户一起使用时有什么关系?
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