Outlook – automatic export and calendar

Welcome Forums General PowerShell Q&A Outlook – automatic export and calendar

Viewing 43 reply threads
  • Author
    Posts
    • #216012
      Participant
      Topics: 3
      Replies: 21
      Points: 67
      Rank: Member

      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

       

    • #216156
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      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.

    • #216237
      Participant
      Topics: 3
      Replies: 21
      Points: 67
      Rank: Member

      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”)

    • #216474
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      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!

    • #216516
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      OK it is quite ugly, hopefully someone comes along and updates with a cleaner answer. Here is what I got to work.

      Add-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!

    • #216558
      Participant
      Topics: 3
      Replies: 21
      Points: 67
      Rank: Member

      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

    • #216576
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      It seems like $olCalendarNav is empty. Maybe it depends on outlook being open? Did you test with outlook open or closed?

    • #216579
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      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?

    • #216585
      Participant
      Topics: 3
      Replies: 21
      Points: 67
      Rank: Member

      I’m tested with outlook open an outlook close, but error stay

    • #216624
      Participant
      Topics: 3
      Replies: 21
      Points: 67
      Rank: Member

      I would like to get an ics from shared calendar.

    • #216642
      Participant
      Topics: 3
      Replies: 21
      Points: 67
      Rank: Member

      if necessary, I can edit my rights in the shared calendar

    • #216678
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      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.

      
      Function Export-OutlookSharedCalendar{
      <#
      .Synopsis
      Allows a user to export a shared calendar from their outlook.
      
      .DESCRIPTION
      Allows a user to export a shared calendar from their outlook.
      
      .NOTES
      Name: Export-OutlookSharedCalendar.ps1
      Author: Doug Maurer
      Version: 1.0.0.3
      DateCreated: 2020-04-08
      DateUpdated: 2020-04-08
      
      .LINK
      
      .INPUTS
      None
      
      .OUTPUTS
      An ics file of the calendar
      
      .PARAMETER Owner
      The actual owner of the shared calendar
      
      .PARAMETER Path
      Full path for the exported file, including the filename.
      
      .PARAMETER StartDate
      The start date of the desired export period
      
      .PARAMETER EndDate
      The end date of the desired export period
      
      .PARAMETER Detail
      The level of calendar detail to export.
      
      .PARAMETER RestrictToWorkingHours
      Used to restrict the export to working hours
      
      .PARAMETER IncludePrivateDetails
      Switch for including private details of the calendar items
      
      .PARAMETER IncludeAttachments
      Switch for including attachments with the calendar items
      
      .EXAMPLE
      Export-OutlookSharedCalendar -Owner 'first.last@contoso.com' -Path 'c:\temp\contoso shared calendar.ics' -startdate 01/01/2019 -enddate 09/01/2019 -detail FullDetails -AllowClobber
      
      Description
      -----------
      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'
      
      .EXAMPLE
      Export-OutlookSharedCalendar -Owner 'first.last@contoso.com' -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 types
      Add-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 settings
      if($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 = $startdate
      if($enddate){
      $CalendarSharing.endDate = $enddate
      }else{
      $CalendarSharing.endDate = (get-date)
      }
      } else {
      $CalendarSharing.startDate = [datetime]"01/01/1970"
      $CalendarSharing.endDate = (get-date)
      }
      
      #export the calendar
      if($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!

       

    • #216705
      Participant
      Topics: 3
      Replies: 21
      Points: 67
      Rank: Member

      Thank you very much for your time willingness and effort, I do not understand why errors are still displayed

      obrazek

    • #216711
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      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 = “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.
      $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)

    • #216735
      Participant
      Topics: 3
      Replies: 21
      Points: 67
      Rank: Member

      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 1 month, 3 weeks ago by taps82.
    • #216741
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      Ok run these lines and paste the output. Be sure to update the owner email.

      $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()
      
      
    • #216750
      Participant
      Topics: 3
      Replies: 21
      Points: 67
      Rank: Member

      output show
      first line : mailbox owner
      second line: True

    • #216759
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      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?

    • #216831
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      OK give this one a try if you can

      #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)
      
      

       

    • #216852
      Participant
      Topics: 3
      Replies: 21
      Points: 67
      Rank: Member

      I have Exchange 2010 and Microsoft Outlook 2013

    • #216855
      Participant
      Topics: 3
      Replies: 21
      Points: 67
      Rank: Member

      error same

    • #216858
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      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?

    • #216870
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      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)
      
      
    • #217125
      Participant
      Topics: 3
      Replies: 21
      Points: 67
      Rank: Member

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

    • #217131
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      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.

    • #217134
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      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.

    • #217164
      Participant
      Topics: 3
      Replies: 21
      Points: 67
      Rank: Member

      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

    • #217179
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

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

    • #217242
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

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

    • #217908
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      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)
      
      
    • #217920
      Participant
      Topics: 3
      Replies: 21
      Points: 67
      Rank: Member

      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
      • This reply was modified 1 month, 3 weeks ago by taps82.
      • This reply was modified 1 month, 3 weeks ago by taps82.
    • #217929
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      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.

    • #218124
      Participant
      Topics: 3
      Replies: 21
      Points: 67
      Rank: Member

      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

      • This reply was modified 1 month, 2 weeks ago by taps82.
      • This reply was modified 1 month, 2 weeks ago by taps82.
    • #218625
      Participant
      Topics: 3
      Replies: 21
      Points: 67
      Rank: Member

      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)

    • #218628
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      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.

    • #218634
      Participant
      Topics: 3
      Replies: 21
      Points: 67
      Rank: Member

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

    • #218646
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      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.

      https://gist.github.com/krzydoug/f624cc7cec81fd006e1230907b74b446

    • #218760
      Participant
      Topics: 3
      Replies: 21
      Points: 67
      Rank: Member

      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"."

    • #218766
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      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.

    • #218769
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      Ok, ok.. last one.. again. 🙂

      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)
      
      
    • #218790
      Participant
      Topics: 3
      Replies: 21
      Points: 67
      Rank: Member

      I code tested only windows 7 / powershell version 2.0. I tried last code, but error same

    • #218799
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      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

    • #218829
      Participant
      Topics: 3
      Replies: 21
      Points: 67
      Rank: Member

      Super! Upgrade powershell resolved problem with com object.Thank you very much!

      You might want to note in the script that it is functional since the version of powershell

      • This reply was modified 1 month, 2 weeks ago by taps82.
    • #218928
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      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.

Viewing 43 reply threads
  • You must be logged in to reply to this topic.