Outlook - automatic export and calendar

error same

Alright, I changed the recipient creation from the namespace to the session.

#fill this out
$owner = “user@email.com”
$path = “c:\temp\test.ics”

Add-Type -AssemblyName ‘Microsoft.Office.Interop.Outlook’

$outlook = New-Object -ComObject outlook.application
$namespace = $outlook.GetNameSpace(‘MAPI’)
$calendarowner = $owner # can be the full smtp address (what i prefer), display name, or alias.
$recipient = $outlook.Session.CreateRecipient($owner)
$null = $recipient.Resolve()
$olFolderCalendar = [Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderCalendar
$CalendarFolder = $outlook.Session.GetSharedDefaultFolder($recipient, $olFolderCalendar)
$calendarsharing = $CalendarFolder.GetCalendarExporter()
$CalendarSharing.CalendarDetail = 2
$CalendarSharing.IncludeAttachments = $false
$CalendarSharing.IncludePrivateDetails = $false
$CalendarSharing.RestrictToWorkingHours = $false
$CalendarSharing.startDate = (get-date).AddDays(-365)
$CalendarSharing.endDate = (get-date)
$MailItem = $CalendarSharing.SaveAsICal($Path)

Is there a chance that the owner you’re passing in isn’t the owner of any of your shared calendars?

This one also works perfect for me.

#fill this out
$owner = “user@email.com”
$path = “c:\temp\test.ics”

Add-Type -AssemblyName ‘Microsoft.Office.Interop.Outlook’

$outlook = New-Object -ComObject outlook.application
$namespace = $outlook.GetNameSpace(‘MAPI’)
$olFolderCalendar = [Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderCalendar
$recipient = $outlook.Session.CreateRecipient($owner)
$null = $recipient.Resolve()
$CalendarFolder = $recipient.Session.GetSharedDefaultFolder($recipient, $olFolderCalendar)
$calendarsharing = $CalendarFolder.GetCalendarExporter()
$CalendarSharing.CalendarDetail = 2
$CalendarSharing.IncludeAttachments = $false
$CalendarSharing.IncludePrivateDetails = $false
$CalendarSharing.RestrictToWorkingHours = $false
$CalendarSharing.startDate = (get-date).AddDays(-365)
$CalendarSharing.endDate = (get-date)
$MailItem = $CalendarSharing.SaveAsICal($Path)

I tried, but error same. Basic problem in can’t convert the com object to recipient ?

Since the recipient resolves just fine, my guess is you have the wrong recipient. Such as you think it’s bob@soandso.com’s calendar but it’s actually bill@soandso.com calendar. I would try to confirm 100% the owner of that calendar. One of these, if not all, should work.

When I input a valid email that resolves, but I don’t have a thing from them in my outlook, I get the following error.

$namespace.GetSharedDefaultFolder($recipient, $olFolderCalendar)
The operation failed because of a registry or installation problem. Restart Outlook and try again. If the problem persists, reinstall.
At line:1 char:1
+ $CalendarFolder = $namespace.GetSharedDefaultFolder($recipient, $olFo ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

I even put in an email that I know I didn’t have shared calendar for, and it found a shared note from that user with this same code. My gut is telling me we are just inputting the wrong owner.

I found that the owner was completely missing from the calendar. I refilled it. So far, the script has the same error. I’ll try it tomorrow

There shouldn’t be anything to fill in. Whoever created the calendar and shared it out, is whose email you need to put in.

Anyone else able to try any of these to export a shared calendar?

Greetings, I’ve been reading more about this and I’d like to have you test if this code works for you.

#fill this out
$owner = “user@email.com”
$path = “c:\temp\test.ics”

Add-Type -AssemblyName ‘Microsoft.Office.Interop.Outlook’

$outlook = New-Object -ComObject outlook.application
$namespace = $outlook.GetNameSpace(‘MAPI’)
$olFolderCalendar = [Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderCalendar
$recipient = $namespace.Session.CreateRecipient($owner)
$null = $recipient.Resolve()
$CalendarFolder = $namespace.Session.GetSharedDefaultFolder($recipient, $olFolderCalendar)
$calendarsharing = $CalendarFolder.GetCalendarExporter()
$CalendarSharing.CalendarDetail = 2
$CalendarSharing.IncludeAttachments = $false
$CalendarSharing.IncludePrivateDetails = $false
$CalendarSharing.RestrictToWorkingHours = $false
$CalendarSharing.startDate = (get-date).AddDays(-365)
$CalendarSharing.endDate = (get-date)
$MailItem = $CalendarSharing.SaveAsICal($Path)

Hello, I tested code, but return error

Cannot convert argument "0", with value: "System.__ComObject", for "GetSharedDefaultFolder" to type "Microsoft.Office.Interop.Outlook.Recipient": "Cannot convert the "System.__ComObject" value of type "Syst
em.__ComObject#{00063045-0000-0000-c000-000000000046}" to type "Microsoft.Office.Interop.Outlook.Recipient"."
At C:\powershell\outlook\test.ps1:13 char:60
+ $CalendarFolder = $namespace.Session.GetSharedDefaultFolder <<<< ($recipient, $olFolderCalendar)
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

The only thing I can think of is that the profile you are loading into doesn’t have a shared mailbox for the email you’re inputting. That’s the only time I have errors. I am going to see if I can get more people to test. Hopefully someone can shed some more light on it in the meantime.

Can you show how config your shared calendar? I would try to compare it to my settings. Cannot a firewall or antivirus cause the problem?

Exchange 2010 is located on a virtual server within VMware

 

Thank you

Hello, this code it si works! Why didn’t the solution from you work?

$path = “C:\temp\calendar.ics”
$LookupCalender = “user”

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
$MyOL = New-Object MyOL
$olInbox = $MyOL.GetCalendar($LookupCalender)
$calendarsharing = $olInbox.GetCalendarExporter()
$CalendarSharing.CalendarDetail = 0
$CalendarSharing.IncludeAttachments = $false
$CalendarSharing.IncludePrivateDetails = $false
$CalendarSharing.RestrictToWorkingHours = $false
$CalendarSharing.startDate = (get-date)
$CalendarSharing.endDate = (get-date).AddDays(+365)
$MailItem = $CalendarSharing.SaveAsICal($Path)

That’s a good question, I can’t explain it. Also, I’m not sure how the code you posted is doing what you want when your original request you state

full details
and according to the documentation
olFreeBusyAndSubject 1 Free/busy information and the appointment subjects are exported to the iCalendar file.
olFreeBusyOnly 0 Only free/busy information is exported to the iCalendar file.
olFullDetails 2 Full details of each appointment item are exported to the iCalendar file.
you are only getting free/busy information. Either way I am glad you got it sorted.

Code is work if set $CalendarSharing.CalendarDetail=2. The problem will probably be somewhere else

That is correct. I just wanted to clarify for future readers that what you posted will not get full details until you change the 0 to 2.

Thank you for posting your solution. I was able to reproduce error when trying to force recipient to cast as a recipient. I believe that will solve the issue for you. Please if you will, try this last attempt to export your calendars. Alternatively, I updated and enhanced the Export-OutlookSharedCalendar function and saved it to github for others to use. Please feel free to give it a try as well. It can make it easy to back up different calendars with different criteria without having to hard code a script.

#fill this out
$calendarowner = "User" # can be the full smtp address (what i prefer), display name, or alias.
$path = "c:\temp\test.ics"

Add-Type -AssemblyName 'Microsoft.Office.Interop.Outlook'

$outlook = New-Object -ComObject outlook.application
$namespace = $outlook.GetNameSpace('MAPI')
$recipient = [Microsoft.Office.Interop.Outlook.Recipient] -as [type]
$calendarfolder = [Microsoft.Office.Interop.Outlook.MAPIFolder] -as [type]
$recipient = $namespace.CreateRecipient($calendarowner)
$null = $recipient.Resolve()
$olFolderCalendar = [Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderCalendar
$CalendarFolder = $namespace.GetSharedDefaultFolder($recipient, $olFolderCalendar)
$calendarsharing = $CalendarFolder.GetCalendarExporter()
$CalendarSharing.CalendarDetail = 2
$CalendarSharing.IncludeAttachments = $false
$CalendarSharing.IncludePrivateDetails = $false
$CalendarSharing.RestrictToWorkingHours = $false
$CalendarSharing.startDate = (get-date).AddDays(-365)
$CalendarSharing.endDate = (get-date)
$MailItem = $CalendarSharing.SaveAsICal($Path)

One thing the function will try to do that neither your solution or this one will is unload and release the resources. I encourage you to try either and/or both out and let me know.

Thank you . I tested your last code, but return error

Cannot convert argument “0”, with value: “System.__ComObject”, for “GetSharedDefaultFolder” to type “Microsoft.Office.Interop.Outlook.Recipient”: “Cannot convert the “System.__ComObject” value of type “System.__ComObject#{00063045-0000-0000-c000-000000000046}” to type “Microsoft.Office.Interop.Outlook.Recipient”.”

Hi Taps,

That is unbelievable. What powershell version are you using? Did you have a chance to try the function? How many different systems have you tested on? It’s going to drive me nuts!

Is anyone else able to test this and/or explain why this is failing there? I can hardly get it to fail on everything I’ve tested. The good thing is you have a working solution, so if you aren’t able to test anymore I understand.

Ok, ok… last one… again. :slight_smile:

I think this one might work.

#fill this out
$calendarowner = "User" # can be the full smtp address (what i prefer), display name, or alias.
$path = "c:\temp\test.ics"

Add-Type -AssemblyName 'Microsoft.Office.Interop.Outlook'

$outlook = New-Object -ComObject outlook.application
$namespace = $outlook.GetNameSpace('MAPI')
$recipient = [Microsoft.Office.Interop.Outlook.Recipient] -as [type]
$calendarfolder = [Microsoft.Office.Interop.Outlook.MAPIFolder] -as [type]
$recipient = $namespace.CreateRecipient($calendarowner)
$null = $recipient.Resolve()
$CalendarFolder = $namespace.GetSharedDefaultFolder($recipient, 9)
$calendarsharing = $CalendarFolder.GetCalendarExporter()
$CalendarSharing.CalendarDetail = 2
$CalendarSharing.IncludeAttachments = $false
$CalendarSharing.IncludePrivateDetails = $false
$CalendarSharing.RestrictToWorkingHours = $false
$CalendarSharing.startDate = (get-date).AddDays(-365)
$CalendarSharing.endDate = (get-date)
$MailItem = $CalendarSharing.SaveAsICal($Path)