簡體   English   中英

Powershell COM對象

[英]Powershell COM objects

我試圖通過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“類型”System“的值。 __ComObject#{00063045-0000-00 00-c000-000000000046}“輸入”Microsoft.Office.Interop.Outlook.Recipient“。” 在行:1 char:34 + $ namespace.GetSharedDefaultFolder <<<<($ recipient,“olFolderCalendar”)+ CategoryInfo:NotSpecified:(:) [],MethodException + FullyQualifiedErrorId:MethodArgumentConversionInvalidCastArgument

我已經嘗試直接將$ recip轉換為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命名空間對象(也是先前設置的)。

嘗試用數字9替換olFolderCalendar
COM對象需要實際值。 它們無法將明文名稱轉換為常量值。

在這里找到了解決方案: 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM