So, I’m once in a bluemoon I end up with a 8AM surprise calendar event when I am out of the office the day before. I threw this little gem together using powershell that will send me an email and a text message to my phone throughout the day if one of these meetings appear the next day in the bright AM. Better that then to be supprised that your already 5 minutes behind on a meeting that started at 8AM by the time you get to your Emal and Calendar for the morning.

#https://download.microsoft.com/download/8/9/9/899EEF2C-55ED-4C66-9613-EE808FCF861C/EwsManagedApi.msi
#Declare Variables
$EWSDLL = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
$MBX = "YourEmail@Domain.org"
$EWSURL = "https://FQDN.Domain.Com/EWS/Exchange.asmx"
$StartDate = (Get-Date)
$EndDate = (Get-Date).AddDays(1)  

#Binding of the calendar of the Mailbox and EWS
Import-Module -Name $EWSDLL
$mailboxname = $MBX
$service = new-object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.Exchangeversion]::exchange2010)
$service.Url = new-object System.Uri($EWSURL)
$userName="SamAccountIDWithoutDomain"
$password="Your Email password"
#Uncomment to use Auth in lieu of passthrough
#$service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList $userName, $password
$folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar,$MailboxName) 
$Calendar = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)  
$Recurring = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition([Microsoft.Exchange.WebServices.Data.DefaultExtendedPropertySet]::Appointment, 0x8223,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Boolean); 
$psPropset= new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)  
$psPropset.Add($Recurring)
$psPropset.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text;

$RptCollection = @()

$AppointmentState = @{0 = "None" ; 1 = "Meeting" ; 2 = "Received" ;4 = "Canceled" ; }

#Define the calendar view  
$CalendarView = New-Object Microsoft.Exchange.WebServices.Data.CalendarView($StartDate, $EndDate, 1000)    
$fiItems = $service.FindAppointments($Calendar.Id,$CalendarView)
if($fiItems.Items.Count -gt 0){
 $type = ("System.Collections.Generic.List"+'`'+"1") -as "Type"
 $type = $type.MakeGenericType("Microsoft.Exchange.WebServices.Data.Item" -as "Type")
 $ItemColl = [Activator]::CreateInstance($type)
 foreach($Item in $fiItems.Items){
  $ItemColl.Add($Item)
 } 
 [Void]$service.LoadPropertiesForItems($ItemColl,$psPropset)  
}

$min = Get-Date '08:00'
$max = Get-Date '08:29'

foreach($Item in $fiItems.Items){      
 $rptObj = "" | Select StartTime,EndTime,Duration,Type,Subject,Location,Organizer,Attendees,AppointmentState,Notes,HasAttachments,IsReminderSet
 $rptObj.StartTime = $Item.Start  
 $rptObj.EndTime = $Item.End  
 $rptObj.Duration = $Item.Duration
 $rptObj.Subject  = $Item.Subject   
 $rptObj.Type = $Item.AppointmentType
 $rptObj.Location = $Item.Location
 $rptObj.Organizer = $Item.Organizer.Address
 $rptObj.HasAttachments = $Item.HasAttachments
 $rptObj.IsReminderSet = $Item.IsReminderSet
 $aptStat = "";
 $AppointmentState.Keys | where { $_ -band $Item.AppointmentState } | foreach { $aptStat += $AppointmentState.Get_Item($_) + " "}
 $rptObj.AppointmentState = $aptStat 
 $RptCollection += $rptObj
 foreach($attendee in $Item.RequiredAttendees){
  $atn = $attendee.Address + "; "  
  $rptObj.Attendees += $atn
  }
 foreach($attendee in $Item.OptionalAttendees){
  $atn = $attendee.Address + "; "  
  $rptObj.Attendees += $atn
 }
 foreach($attendee in $Item.Resources){
  $atn = $attendee.Address + "; "  
  $rptObj.Resources += $atn
 }
 $rptObj.Notes = $Item.Body.Text
#Display on the screen
 #"Start:   " + $Item.Start  
 #"Subject: " + $Item.Subject 


 if ($min.TimeOfDay -le $rptObj.StartTime.TimeOfDay -and $max.TimeOfDay -ge $rptObj.StartTime.TimeOfDay) {

        $message = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage $Service
        $message.Subject = "REMINDER!! " + $Item.Subject          
        $message.Body = new-object Microsoft.Exchange.WebServices.Data.MessageBody([Microsoft.Exchange.WebServices.Data.BodyType]::Text, ($rptObj | Format-List | Out-String) -replace '\n(?=")','<br/>')

        foreach ($file in $Attachment) {
            $null = $message.Attachments.AddFileAttachment($file)
        }
        foreach ($recipient in $To) {
            $null = $message.ToRecipients.Add(
                $recipient    
            )
        }
        foreach ($recipient in $Cc) {
            $null = $message.CcRecipients.Add(
                $recipient
            )
        }
        foreach ($recipient in $Bcc) {
            $null = $message.BccRecipients.Add(
                $recipient
            )
        }
        $message.ToRecipients.Add("ToWhomeGetsTheReminder@Domain.Com")
        $message.ToRecipients.Add("9998887777@vtext.com")
        $message.SendAndSaveCopy()

        $PR_DEF_POST_MSGCLASS_W = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x36E5,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String)
        $PR_DEF_POST_DISPLAYNAME_W = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x36E6,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String)
        $PR_DEFERRED_SEND_TIME = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(16367, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::SystemTime)

        $message.Subject = "2ND REMINDER!! " + $Item.Subject
        $sendTime = [System.DateTime]::Now.AddHours(2)
        $message.SetExtendedProperty($PR_DEFERRED_SEND_TIME, $sendTime)
        $message.SendAndSaveCopy()

        $message.Subject = "LAST REMINDER!! " + $Item.Subject
        $sendTime = [datetime]::ParseExact("07:00","hh:mm",$null).AddDays(1)
        $message.SetExtendedProperty($PR_DEFERRED_SEND_TIME, $sendTime)
        $message.SendAndSaveCopy()
        #$message
        #$message.ExtendedProperties  | Format-List | Out-String
        #Write-Host $rptObj | Format-List | Out-String
 }

}   
#Export to a CSVFile
#$RptCollection |  Export-Csv -NoTypeInformation -Path "c:\$MailboxName-CalendarCSV.csv"
#pause

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax