none
Creating All Day Events - Incorrect Date RRS feed

  • Question

  • Hello,

    I have been tasked with creating company holidays for our US employees. I have the script like 98% but I have one last thing to get past and I just can't figure it out. I want the holiday (labor day for example) to simply say "Labor Day". However, it is saying 12AM Labor Day. 

    When I look at the entry, it displays with a start time of the Mon 9/5/2016 12:00AM (greyed) and the end time as the Sun 9/4/2016 12:00AM (Greyed) (what?). If I open the event, the All Day checkbox is checked. If I uncheck it and then check it again and save the event, it corrects the entry. 

    The CSV has a start column and an end column and those are the same date of 9/5/2016 

    Here is my code:

    Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll" 

    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList ([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2) 
    $service.Url = [system.URI] "https://SERVER/ews/exchange.asmx"

    $calendaritems = Import-CSV "calendaritems.csv" 
    $users = import-csv “users.csv”

    foreach($user in $users){ 
        $service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId –ArgumentList ([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,$user.PrimarySMTPAddress)

        foreach($item in $calendaritems){ 
            $appointment = New-Object Microsoft.Exchange.WebServices.Data.Appointment -ArgumentList $service 
            $appointment.Subject = $item.Subject 
            $appointment.Body = $item.Body 
            $appointment.Start = $item.Start 
            $appointment.End = $item.End 
            $appointment.Location = $item.Location
            $appointment.LegacyFreeBusyStatus = "Free" 
            $appointment.IsReminderSet = $false
            $appointment.IsAllDayEvent = $True

            $appointment.Save([Microsoft.Exchange.WebServices.Data.SendInvitationsMode]::SendToNone)
        }

    Please Help, it is appreciated!!!


    Thursday, January 14, 2016 4:50 PM

Answers

  • You need to look closely at your script $EventDate was just an example I gave you it has no meaning in your script which means you just passing in a null value.

    You should be using something like

           $Appointment.Start = [System.DateTime]::Parse($item.Start)
           $Appointment.End = [System.DateTime]::Parse($Appointment.Start).AddDays(1)

    This will mean you will have the variable your trying to use linked to an actual CSV field.

    Cheers
    Glen

    • Marked as answer by Jthibeault-BT Tuesday, January 26, 2016 2:47 PM
    Monday, January 25, 2016 12:27 AM

All replies

  • For appointments to display correctly (and you should check both OWA and Outlook) getting the correct timezone is critical even though you using Impersonation because you aren't specifically setting the TimeZone it will then come from the machine you executing the script on. I would suggest you always use the

    			$Appointment.StartTimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneById($TimeZone)
    			$Appointment.EndTimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneById($TimeZone)

    Properties and set that TimeZone to that of the Timezone the Mailboxes are in (which may vary if you users are spread geographically). If you want to try to dynamically find the TimeZone of the Mailbox this may help http://gsexdev.blogspot.com/2015/11/finding-timezone-being-used-in-mailbox.html

    Cheers
    Glen

    Friday, January 15, 2016 1:39 AM
  • Thanks Glen!

    However, I tried adding this to my script and unfortunately, no difference was made.

     $appointment.StartTimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Eastern Standard Time")

    $appointment.EndTimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Eastern Standard Time"

    What I get when I add the above code:

    OWA:

    - Title: 12:00AM 12:00AM Labor Day, US

    - Location: US

    - All Day Unchecked

    - Time from Mon sep 5 12:00AM, 2016 to Mon sep 5, 2016 12:00AM


    Outlook:

    - Title: 12:00AM Labor Day, US

    - Location: US

    - All Day Checked

    - Time from Mon sep 5 12:00AM, 2016 to Sun sep 4, 2016 12:00AM

    iPhone: 

    - Title: Labor Day

    - Location: US

    - All Day from Mon sep 5, 2016 to Sun, Sep 4 2016

    There is no difference when I comment out that section, or if I include it. We are on Exchange 2010 SP 3 and I am running EWS API 2.2. 

    Is there anything else that you recommend I look into?

    Thanks!

    Tuesday, January 19, 2016 7:39 PM
  • Check your Start and End Time Where are they coming from ??  all you really need is the start time and the End time should be +24 hours eg

            $appointment.Start = $item.Start 
            $appointment.End =  $item.Start.AddDays(1)

    Cheers
    Glen

    Tuesday, January 19, 2016 10:04 PM
  • Thanks Glen, I have the start and end times in my CSV file in columns called "Start" and "end", and the dates are 9/5/16 and 9/5/16. I tried to input the lines you have given me, but it looks like it failed with the error: Method invocation failed because [system.string] does not contain a method named 'add days'.

    You have been a great help thus far, thanks so much for that oh wise guru!

    Wednesday, January 20, 2016 5:27 PM
  • >>called "Start" and "end", and the dates are 9/5/16 and 9/5/16.

    That's not valid for an all day event it must start one day and finish 24 hours later.

    You might want to try

            $Appointment.Start = [System.DateTime]::Parse($EventDate)
            $Appointment.End = [System.DateTime]::Parse($EventDate).AddDays(1)

    That should ensure the correct DataTypes are used

    Cheers
    Glen

    Thursday, January 21, 2016 5:06 AM
  • Hi Glen,

    Thanks so much for all of the help, and I know you are giving me some great direction but something is not clicking, and I apologize for that. I have included my script below and have some sections commented out that have not worked so I can still refer to them if needed. 

    I hope you can help sir!

    I am getting an error of 

     Exception calling "Parse" with "1" argument(s): "String was not recognized as a valid DateTime."

    At \\HOST\Documents\Scripts\Exchange\PS-DeployCalendarItems.ps1:20 char:9

    +         $Appointment.End = [System.DateTime]::Parse($EventDate).AddDays(1)

    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

        + FullyQualifiedErrorId : FormatException 

    Here is my script right now with what you have given me:

     Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll" 

    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList ([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2) 

    $service.Url = [system.URI] "https://SERVER/ews/exchange.asmx"

    $calendaritems = Import-CSV "calendaritems.csv" 

    $users = import-csv “users.csv”

    foreach($user in $users){ 

        $service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId –ArgumentList ([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,$user.PrimarySMTPAddress)

        foreach($item in $calendaritems){ 

            $appointment = New-Object Microsoft.Exchange.WebServices.Data.Appointment -ArgumentList $service 

            $appointment.Subject = $item.Subject 

            $appointment.Body = $item.Body 

            #$appointment.Start = $item.Start 

            #$appointment.End = $item.Start.AddDays(1)

            #$appointment.End = $item.End

            $Appointment.Start = [System.DateTime]::Parse($EventDate)

            $Appointment.End = [System.DateTime]::Parse($EventDate).AddDays(1)

            $appointment.Location = $item.Location

            $appointment.LegacyFreeBusyStatus = "Free" 

            $appointment.IsReminderSet = $false

            $appointment.IsAllDayEvent = $True

            #$appointment.StartTimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Eastern Standard Time")

    #$appointment.EndTimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Eastern Standard Time")

            

            $appointment.Save([Microsoft.Exchange.WebServices.Data.SendInvitationsMode]::SendToNone)

        }

    }  

    Friday, January 22, 2016 8:41 PM
  • I have also changed the script (see below) and got the error: (see below)

     Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll" 

    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList ([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2) 

    $service.Url = [system.URI] "https://SERVER/ews/exchange.asmx"

    $calendaritems = Import-CSV "calendaritems.csv" 

    $users = import-csv “users.csv”

    $EventDate = $item.start

    foreach($user in $users){ 

        $service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId –ArgumentList ([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,$user.PrimarySMTPAddress)

        foreach($item in $calendaritems){ 

            $appointment = New-Object Microsoft.Exchange.WebServices.Data.Appointment -ArgumentList $service 

            $appointment.Subject = $item.Subject 

            $appointment.Body = $item.Body 

            #$appointment.Start = $item.Start 

            #$appointment.End = $item.Start.AddDays(1)

            #$appointment.End = $item.End

            $Appointment.Start = [System.DateTime]::Parse($EventDate)

            $Appointment.End = [System.DateTime]::Parse($EventDate).AddDays(1)

            $appointment.Location = $item.Location

            $appointment.LegacyFreeBusyStatus = "Free" 

            $appointment.IsReminderSet = $false

            $appointment.IsAllDayEvent = $True

            #$appointment.StartTimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Eastern Standard Time")

    #$appointment.EndTimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Eastern Standard Time")

            

            $appointment.Save([Microsoft.Exchange.WebServices.Data.SendInvitationsMode]::SendToNone)

        }

    }  

    Exception calling "Parse" with "1" argument(s): "String was not recognized as
    a valid DateTime."
    At C:\Scripts\Exchange\PS-DeployCalendarItems.ps1:22 char:9
    +         $Appointment.End = [System.DateTime]::Parse($EventDate).AddDays(1)
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : FormatException

    Friday, January 22, 2016 9:14 PM
  • You need to look closely at your script $EventDate was just an example I gave you it has no meaning in your script which means you just passing in a null value.

    You should be using something like

           $Appointment.Start = [System.DateTime]::Parse($item.Start)
           $Appointment.End = [System.DateTime]::Parse($Appointment.Start).AddDays(1)

    This will mean you will have the variable your trying to use linked to an actual CSV field.

    Cheers
    Glen

    • Marked as answer by Jthibeault-BT Tuesday, January 26, 2016 2:47 PM
    Monday, January 25, 2016 12:27 AM
  • Glen, thank you so much! You rock my friend! It worked perfectly!
    Tuesday, January 26, 2016 2:47 PM