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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
#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 |