Powershell COM Objects

I am trying to get calendar items from a shared calendar through Powershell with the following code:

$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") 

but I get a type mismatch error:

Cannot convert argument "0" with value: "System .__ ComObject", for "GetSharedDefaultFolder" enter "Microsoft.Office.I nterop.Outlook.Recipient": "Cannot convert value" System .__ ComObject "to type" System .__ ComObject # {00063045-0000-00 00-c000-000000000046} "to print" Microsoft.Office.Interop.Outlook.Recipient "." On line: 1 char: 34 + $ namespace.GetSharedDefaultFolder <<<($ recipient, "olFolderCalendar") + CategoryInfo: NotSpecified: (:) [], MethodException + FullyQualifiedErrorId: MethodArgumentConversionInvalidCastArgument

I tried pouring $ recipient directly into Microsoft.Office.Interop.Outlook.Recipient , which does not work, and I also tried the invoke-method() procedure, well described here: http://www.mcleod.co.uk/scotty/ powershell / COMinterop.htm

It seems that the latter should work, but it does not seem to have provisions for the many parameters that GetSharedDefaultFolder() requires.

+4
source share
3 answers

Found a solution here: 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
source

I managed to get this working using the InvokeMember System .__ ComObject method. To pass multiple parameters to a method, simply enclose them in parentheses.

An example line of code is shown here:

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

$ user is the recipient object that was previously configured. $ mapi is a MAPI namespace object (also installed earlier).

+3
source

Try replacing olFolderCalendar with number 9 .
COM objects need actual values. They cannot convert transparent text names to constant values.

0
source

Source: https://habr.com/ru/post/1345763/


All Articles