2011-03-29 100 views
4

我試圖通過PowerShell來獲得與下面的代碼共享日曆的日曆項目:PowerShell的COM對象

$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

我已經試圖直接將$收件人轉換爲Microsoft.Office.Interop.Outlook.Recipient,這不起作用,並且我也嘗試了invoke-method()程序,這裏詳細記錄了這裏:http://www.mcleod.co.uk/scotty/powershell/COMinterop.htm

看來後者應該可以工作,但它似乎沒有關於GetSharedDefaultFolder()要求的多個參數的規定。

回答

0

找到了解決辦法在這裏: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 
+0

與多個smtp和Exchange帳戶一起使用時Outlook有什麼用處? – Kiquenet 2017-04-12 13:23:32

0

嘗試用9代替olFolderCalendar
COM對象需要實際值。他們不能將明文名稱轉換爲常量值。

+0

錯誤處理收件人對象,而不是文件夾對象。 – JakeRobinson 2011-03-30 13:41:31

+1

[Microsoft.Office.Interop.Outlook.OlDefaultFolders] :: olFolderCalendar也應該可以工作 – 2012-08-13 15:15:24

3

我設法得到這個工作使用System .__ ComObject的InvokeMember方法。爲了將多個參數傳遞給方法,只需將它們括在圓括號中。

的代碼行的一個例子如下所示:

PS C:> $ usercontacts = [系統.__ ComObject] .InvokeMember( 「GetSharedDefaultFolder」[System.Reflection.BindingFlags] ::的InvokeMethod,$空,$ mapi,($ user,10))

$ user是先前設置的收件人對象。 $ mapi是MAPI命名空間對象(以前也設置過)。