Click an Ad

If you find this blog helpful, please support me by clicking an ad!

Friday, March 7, 2014

"Automating" Emailing a Daily Calendar

One of my colleagues needed something to email his daily schedule to his boss. I'm not really that well versed on pulling data out of Microsoft Office, but I figured I'd give it a go.

IMPORTANT: Outlook must be running in order to execute this! I know that's not ideal, but my plan is to have a shortcut to the script (as I wrote about Wednesday) so that the user can finalize their schedule for the day, then double-click to send the schedule.

#-----------------------BEGIN SCRIPT--------------------------

#Variables - Modify as needed
$TodaysDate = (Get-Date -format d)
$From = "user@contoso.com"
$To = "manager@contoso.com"
$SMTPServer = "smtpserver.contoso.com"
$EmailSubject = "User's Schedule for Today"

#Connect to Outlook session, and grab the calendar
Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
$olFolders = "Microsoft.Office.Interop.Outlook.OlDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
$folder = $namespace.getDefaultFolder($olFolders::olFolderCalendar)

#Initialize empty array for the appointments
$Appointments = @()

#Go through the appointments and grab any of them that occur today
#Then append those to the array
Foreach ($Item in $Folder.items){
If (($Item.start.ToShortDateString()) -eq $TodaysDate){
$Appointments += ($Item | select Organizer, ConversationTopic, Start, End)
} #End If
} #End Foreach

#Convert the array to HTML, then string for use in the email
$AppointmentsHTMLString = $Appointments | sort Start | ConvertTo-HTML | out-string

#Send the email
Send-MailMessage -From $From -To $To -Subject $EmailSubject -BodyAsHTML -Body $AppointmentsHTMLString -SMTPServer $SMTPServer

#-----------------------END SCRIPT--------------------------

No comments:

Post a Comment