Like my other PowerShell blog posts, this is a sample of what a script can do.  This post is really meant for my PFE co-workers.  I posted recently about using Outlook to simply list out calendar appointments with a basic filter.  I finally had some time today during travel to Chicago to work on the script I was beginning to play with when I wrote the other post.  This script looks in my default calendar for appointments that I use to keep track of customer visits.  It also looks for special appointments that this script creates to keep it from creating duplicates if its run more than once.

The script finds each one of these "dispatch" by their name and only looks at the last 6 days.  I create a hash table ( @{} ) to store a quick list of the existing appointments that past runs of this script has already created.  I populate it by looping through the list of found auto-appointments.  Once I have all that, I loop through the dispatch appointments looking to see if the hash table has an entry for the appointment we want to ensure either already exists or we need to create.  If its not found, we go ahead and create a new appointment item for 6 days in the future to remind me of a deadline associated with each onsite visit.

I don't have a lot of bells and whistles here.  Its a basic concept script that does work on my machine.  I plan to take this start and make it into a more production level script to run as a regular task for myself, basically each time I get a new "dispatch" on my calendar.  I suspect some of my PFE buds will like this as well.

I changed the prefixes in the sample script to obscure the info we actually have in our calendars, so PFE folks reading this, just ping me and I can physically send you this script with the proper strings in there for this work right.

 

$Outlookapp = new-object -comobject outlook.application

$allCalItems = $Outlookapp.GetNamespace("MAPI").GetDefaultFolder(9).items

$dispatches = $allCalItems | where-object{$_.subject -like "Prefix*" -and $_.end -gt (Get-Date).Adddays(-7)}

$ReportDueItems = $allCalItems | where-object{$_.subject -like "NewItemPrefix*"}

 

#$reportdueitems | Format-Table subject

 

$ExistingTripReportDueItems = @{}

  

foreach ($ReportDueItem in $reportdueitems)

{

     If ($ExistingTripReportDueItems[$reportdueitem.subject] -eq $null) {$ExistingTripReportDueItems[$reportdueitem.subject] = $reportdueitem.end}

}

  

 

foreach ($dispatch in $dispatches)

{

     If ($ExistingTripReportDueItems["NewItemPrefix - " + $dispatch.subject] -eq $null)

     {

          $newitem = $outlookapp.createItem(1)

          $newitem.subject = [string]("NewItemPrefix - " + $dispatch.subject)

          $newitem.start = ([datetime]($dispatch.end)).AddDays(6)

          $newitem.Alldayevent = $true

          $newitem.Save()

          "Just Created new calendar item: NewItemPrefix - " + $dispatch.subject

     }

     Else

     {

          Write-Host -ForegroundColor Yellow "Item Already Present: " $dispatch.subject

     }

}

  

During the brief testing I had to do to debug this concept code, I needed a way to delete all the test appointments I had created (I had many duplicates at first).  I wrote a simple little script that will wipe your calendar of any of these auto-appointments.

#Clean up Auto Appointment Items

$Outlookapp = new-object -comobject outlook.application

$allCalItems = $Outlookapp.GetNamespace("MAPI").GetDefaultFolder(9).items

$dispatches = $allCalItems | where-object{$_.subject -like "NewItemPrefix*"}

$dispatches |  foreach-object {$_.Delete()}

I hope this script ends up being helpful for PFE buds out there, and maybe will show some simple use of the Outlook.application object model.

 

 

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm.