Welcome › Forums › General PowerShell Q&A › Outlook – automatic export and calendar
- This topic has 43 replies, 2 voices, and was last updated 9 months, 1 week ago by
Participant.
-
AuthorPosts
-
-
April 6, 2020 at 7:53 pm #216012
Hello,
I used outlook 2013. When I export shared calendar (permission as owner) I use:
- Click File > Save Calendar.
- This will open a window where you will pick where to save the file to your computer, confirm the file name and click More Options.
- Change the date range so it includes all of the data you would like to export.
- Change the Detail select to Full Details.
- Click OK and then Save the file.
Can you please advise me on how to do these operations in powershell? Thank you
- Click File > Save Calendar.
-
April 7, 2020 at 4:58 am #216156
I’d be more than happy to help you figure it out. I’d need you to post what you’ve tried that’s not working.
-
April 7, 2020 at 9:47 am #216237
I tried this code, but I don’t know load share calendar
Add-Type -AssemblyName microsoft.office.interop.outlook
$olFolders = “Microsoft.Office.Interop.Outlook.OlDefaultFolders” -as [type]
$olCalendarDetail = “Microsoft.Office.Interop.Outlook.olCalendarDetail” -as [type]
$olCalendarMailFormat = “Microsoft.Office.Interop.Outlook.olCalendarMailFormat” -as [type]
$outlook = New-Object -ComObject outlook.application
$namespace = $Outlook.GetNameSpace(“mapi”)
$folder = $namespace.getDefaultFolder($olFolders::olFolderCalendar)
$CalendarSharing=$folder.GetCalendarExporter()
$CalendarSharing.CalendarDetail = $olCalendarDetail::olFreeBusyOnly
$CalendarSharing.startDate = Get-Date
$CalendarSharing.endDate = (Get-Date).addDays(365)
$CalendarSharing.RestrictToWorkingHours = $false
$CalendarSharing.IncludeAttachments = $false
$CalendarSharing.IncludePrivateDetails = $false
$MailItem = $CalendarSharing.SaveAsICal(“calendar.ics”) -
April 8, 2020 at 12:38 am #216474
Yeah it seems there is information everywhere about getting the DEFAULT calendar.. I believe you’ll probably need .GetSharedDefaultFolder() or .OpenSharedItem() but I haven’t had success with it yet. I’ll keep trying as I have time. If you figure it out, please share!
-
April 8, 2020 at 5:02 am #216516
OK it is quite ugly, hopefully someone comes along and updates with a cleaner answer. Here is what I got to work.
PowerShell12345678910111213141516Add-Type -assemblyname "microsoft.office.interop.outlook"$olFolders = “Microsoft.Office.Interop.Outlook.OlDefaultFolders” -as [type]$olCalendarMailFormat = “Microsoft.Office.Interop.Outlook.olCalendarMailFormat” -as [type]$outlook = New-Object -ComObject outlook.application$namespace = $Outlook.GetNameSpace(“mapi”)$olCalendarNav = $namespace.Application.ActiveExplorer().navigationpane.modules.getnavigationmodule(1)$sharedcalendars = $olCalendarNav.NavigationGroups.GetDefaultNavigationGroup(2).navigationfolders # 1 = My Calendars, 2 = Shared Calendars, 3 = Other Calendars$targetcalendar = $sharedcalendars | where-object displayname -like 'target calendar*'$CalendarSharing = $targetcalendar.Folder.GetCalendarExporter()$CalendarSharing.CalendarDetail = 2 #You wanted full detail$CalendarSharing.startDate = (Get-Date).addDays(-365)$CalendarSharing.endDate = (Get-Date).addDays(-180)$CalendarSharing.RestrictToWorkingHours = $false$CalendarSharing.IncludeAttachments = $false$CalendarSharing.IncludePrivateDetails = $false$MailItem = $CalendarSharing.SaveAsICal(“c:\temp\calendar.ics”)Take note of the where-object to get the desired shared calendar. You can also target “My Calendars” and “Other Calendars” group as commented. This was really interesting to learn, thanks for asking the question.
Something else you should know is if your powershell is running as admin, your outlook should be too. (or closed) I’ve also had some issues in the past trying to do some of these outlook methods with certain versions of office 365 Outlook.
I hope this helps!
-
April 8, 2020 at 12:07 pm #216558
Thank you very much. I get error
You cannot call a method on a null-valued expression.
$sharedcalendars = $olCalendarNav.NavigationGroups.GetDefaultNavigationGroup <<<< (2).navigationfolders # 1 = My Calendars, 2 = Shared Calendars, 3 = Other Calendars
+ CategoryInfo : InvalidOperation: (GetDefaultNavigationGroup:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull -
April 8, 2020 at 1:12 pm #216576
It seems like $olCalendarNav is empty. Maybe it depends on outlook being open? Did you test with outlook open or closed?
-
April 8, 2020 at 1:18 pm #216579
OK yes it definitely depends on outlook being open. I had a feeling the way I was using the activeexplorer, navigation pane, and navigation folders, was all dependent on the outlook window itself. With outlook closed, first thing I had to do is at the $namespace = mapi command I had to choose an outlook profile. Second thing is have the outlook window open. There has got to be another, better way. Are you needing to do this on your own calendar only? Would the requirement to have outlook open make this not acceptable?
-
April 8, 2020 at 1:35 pm #216585
I’m tested with outlook open an outlook close, but error stay
-
April 8, 2020 at 3:09 pm #216624
I would like to get an ics from shared calendar.
-
April 8, 2020 at 3:33 pm #216642
if necessary, I can edit my rights in the shared calendar
-
April 8, 2020 at 5:02 pm #216678
OK I am not sure how to attach a file here or if i can, so apologies for how long it is. I wrote a function so you can specify what owner, date range, details that you want. I added a couple of examples that should help you to use it. My only fear is the formatting getting messed up from the forum. I need to get this uploaded to the gallery but I want to do a lot more testing first. I was able to use this to export 3 different shared calendars and i tried many combinations of options.
PowerShell123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145Function Export-OutlookSharedCalendar{<#.SynopsisAllows a user to export a shared calendar from their outlook..DESCRIPTIONAllows a user to export a shared calendar from their outlook..NOTESName: Export-OutlookSharedCalendar.ps1Author: Doug MaurerVersion: 1.0.0.3DateCreated: 2020-04-08DateUpdated: 2020-04-08.LINK.INPUTSNone.OUTPUTSAn ics file of the calendar.PARAMETER OwnerThe actual owner of the shared calendar.PARAMETER PathFull path for the exported file, including the filename..PARAMETER StartDateThe start date of the desired export period.PARAMETER EndDateThe end date of the desired export period.PARAMETER DetailThe level of calendar detail to export..PARAMETER RestrictToWorkingHoursUsed to restrict the export to working hours.PARAMETER IncludePrivateDetailsSwitch for including private details of the calendar items.PARAMETER IncludeAttachmentsSwitch for including attachments with the calendar items.EXAMPLEExport-OutlookSharedCalendar -Owner '[email protected]' -Path 'c:\temp\contoso shared calendar.ics' -startdate 01/01/2019 -enddate 09/01/2019 -detail FullDetails -AllowClobberDescription-----------Exports specific items from shared calendar owned by first.last@constoso.com from the default (or chosen) outlook profile and exports it to 'c:\temp\contoso shared calendar.ics'.EXAMPLEExport-OutlookSharedCalendar -Owner '[email protected]' -Path 'c:\users\windowsuser\documents\exporttest.ics' -detail FreeBusyOnly -RestrictToWorkingHours#>[cmdletbinding()]Param([alias('CalendarOwner')][Parameter(Mandatory=$true)]$Owner,[Parameter(Mandatory=$true)]$Path,[datetime]$StartDate,[datetime]$EndDate,[Parameter()][ValidateSet("FreeBusyOnly","FreeBusyAndSubject","FullDetails")]$Detail = "FreeBusyOnly",[switch]$RestrictToWorkingHours = $false,[switch]$IncludeAttachments = $false,[switch]$IncludePrivateDetails = $false,[switch]$AllowClobber = $false)# load the required .NET typesAdd-Type -AssemblyName 'Microsoft.Office.Interop.Outlook'# access Outlook object model$outlook = New-Object -ComObject outlook.application# connect to the appropriate location$namespace = $outlook.GetNameSpace('MAPI')#create a recipient object representing the owner of the shared calendar you want to export$calendarowner = $owner # can be the full smtp address (what i prefer), display name, or alias.$recipient = $namespace.CreateRecipient($calendarowner)$null = $recipient.Resolve()#specify the type of folder object we are wanting to work with$olFolderCalendar = [Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderCalendar#get the specified user/specified default folder$CalendarFolder = $namespace.GetSharedDefaultFolder($recipient, $olFolderCalendar)#Set up the exporter$calendarsharing = $CalendarFolder.GetCalendarExporter()#assign any switches first, because detail may override these settingsif($RestrictToWorkingHours){$CalendarSharing.RestrictToWorkingHours = $true}if($IncludeAttachments){$CalendarSharing.IncludeAttachments = $true}if($IncludePrivateDetails){$CalendarSharing.IncludePrivateDetails = $true}switch($Detail){"FreeBusyOnly" {$CalendarSharing.CalendarDetail = 0$CalendarSharing.IncludeAttachments = $false$CalendarSharing.IncludePrivateDetails = $false}"FreeBusyAndSubject" {$CalendarSharing.CalendarDetail = 1$CalendarSharing.IncludeAttachments = $false$CalendarSharing.RestrictToWorkingHours = $false}"FullDetails" {$CalendarSharing.CalendarDetail = 2$CalendarSharing.RestrictToWorkingHours = $false}}if($startdate){$CalendarSharing.startDate = $startdateif($enddate){$CalendarSharing.endDate = $enddate}else{$CalendarSharing.endDate = (get-date)}} else {$CalendarSharing.startDate = [datetime]"01/01/1970"$CalendarSharing.endDate = (get-date)}#export the calendarif($pathtest = [bool](Test-Path $Path) -and ($AllowClobber -eq $false)){write-error "File $path already exists and AllowClobber not specified";break}if($pathtest){Remove-Item $path -Force}$MailItem = $CalendarSharing.SaveAsICal($Path)}I really hope this helps!
-
April 8, 2020 at 6:17 pm #216705
-
April 8, 2020 at 7:02 pm #216711
OK let’s figure this out. Could you run each of these lines one by one and tell me where you get the first error.
#fill this out
$owner = “[email protected]”
$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 = $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) -
April 8, 2020 at 7:39 pm #216735
first error is
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:\TEMP\test.ps1:13 char:52
+ $CalendarFolder = $namespace.GetSharedDefaultFolder <<<< ($recipient, $olFolderCalendar)
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument-
This reply was modified 9 months, 2 weeks ago by
taps82.
-
This reply was modified 9 months, 2 weeks ago by
-
April 8, 2020 at 7:45 pm #216741
Ok run these lines and paste the output. Be sure to update the owner email.
PowerShell123456789101112$owner = “name@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.write-output $owner$recipient = $namespace.CreateRecipient($calendarowner)$recipient.Resolve() -
April 8, 2020 at 8:07 pm #216750
output show
first line : mailbox owner
second line: True -
April 8, 2020 at 8:23 pm #216759
I assume by mailbox owner you mean the owner’s email. I’m not sure why it’s saying it can’t convert the com object to recipient. What version of exchange and what version of outlook are you using?
-
April 9, 2020 at 5:05 am #216831
OK give this one a try if you can
PowerShell1234567891011121314151617181920#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 = $namespace.CreateRecipient($calendarowner)$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) -
April 9, 2020 at 6:26 am #216852
I have Exchange 2010 and Microsoft Outlook 2013
-
April 9, 2020 at 6:27 am #216855
error same
-
April 9, 2020 at 6:33 am #216858
Alright, I changed the recipient creation from the namespace to the session.
PowerShell123456789101112131415161718192021#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?
-
April 9, 2020 at 6:58 am #216870
This one also works perfect for me.
PowerShell1234567891011121314151617181920#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) -
April 9, 2020 at 3:31 pm #217125
I tried, but error same. Basic problem in can’t convert the com object to recipient ?
-
April 9, 2020 at 3:38 pm #217131
Since the recipient resolves just fine, my guess is you have the wrong recipient. Such as you think it’s [email protected]‘s calendar but it’s actually [email protected] calendar. I would try to confirm 100% the owner of that calendar. One of these, if not all, should work.
-
April 9, 2020 at 3:43 pm #217134
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.
PowerShell1234567$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.COMExceptionI 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.
-
April 9, 2020 at 6:21 pm #217164
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
-
April 9, 2020 at 7:12 pm #217179
There shouldn’t be anything to fill in. Whoever created the calendar and shared it out, is whose email you need to put in.
-
April 9, 2020 at 10:07 pm #217242
Anyone else able to try any of these to export a shared calendar?
-
April 12, 2020 at 2:13 am #217908
Greetings, I’ve been reading more about this and I’d like to have you test if this code works for you.
PowerShell1234567891011121314151617181920#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) -
April 12, 2020 at 8:43 am #217920
Hello, I tested code, but return error
PowerShell123456[crayon-600d5a725d072931127910 inline="true" ]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"."At C:\powershell\outlook\test.ps1:13 char:60+ $CalendarFolder = $namespace.Session.GetSharedDefaultFolder <<<< ($recipient, $olFolderCalendar)+ CategoryInfo : NotSpecified: (:) [], MethodException+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument -
April 12, 2020 at 9:21 am #217929
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.
-
April 13, 2020 at 11:22 am #218124
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
-
April 14, 2020 at 7:52 pm #218625
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) -
April 14, 2020 at 8:00 pm #218628
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.
-
April 14, 2020 at 8:06 pm #218634
Code is work if set $CalendarSharing.CalendarDetail=2. The problem will probably be somewhere else
-
April 14, 2020 at 9:07 pm #218646
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.
PowerShell12345678910111213141516171819202122#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.
https://gist.github.com/krzydoug/f624cc7cec81fd006e1230907b74b446
-
April 15, 2020 at 5:54 am #218760
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"."
-
April 15, 2020 at 6:10 am #218766
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.
-
April 15, 2020 at 6:17 am #218769
Ok, ok.. last one.. again. 🙂
I think this one might work.
PowerShell123456789101112131415161718192021#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) -
April 15, 2020 at 6:55 am #218790
I code tested only windows 7 / powershell version 2.0. I tried last code, but error same
-
April 15, 2020 at 7:01 am #218799
Oh, well version 2.0 is the first thing I’d get rid of. You can get 5.1 for windows 7, the link is below. I sure hope you don’t have any type of rule that you need 2.0, that would be the worst rule I could think of. Also, it seems many others have had odd issues with this COM type conversion error for a while, so 5.1 will probably resolve it.
WMF/Powershell 5.1 – https://www.microsoft.com/en-us/download/details.aspx?id=54616
-
April 15, 2020 at 8:08 am #218829
-
April 15, 2020 at 1:08 pm #218928
That’s awesome. I’m not sure why I didn’t think to ask this question before, I’m sorry for that. It’s always the little details, no? I will add a note to the script, thank you for verifying and responding back. Take care Taps.
-
-
AuthorPosts
- The topic ‘Outlook – automatic export and calendar’ is closed to new replies.