Saturday, 12 October 2013

Get-OLCalendarItem

  1. Function Get-OLCalendarItem { 
  2.  
  3.  
  4. <# 
  5. .SYNOPSIS 
  6.     A function to retreive Outlook Calender items between two dates.  
  7.     Returns PSobjects containing each item. 
  8. .DESCRIPTION 
  9.     The function opens one's outlook calender, then retrieves the items.  
  10.     The function takes 2 parameter: start, end - items are returned that  
  11.     start betweween these two dates. 
  12. .NOTES 
  13.     File Name  : Get-OLCalendarItem 
  14.     Author     : Thomas Lee - tfl@psp.co.uk 
  15.     Requires   : PowerShell Version 3.0 
  16. .LINK 
  17.     This script posted to: 
  18.         http://www.pshscripts.blogspot.com 
  19.      
  20. .EXAMPLE 
  21.     Left as an exercise for the reader      
  22.  
  23. #> 
  24.  
  25. [CmdletBinding()] 
  26. Param ( 
  27. $start = $(Get-Date) , 
  28. $end   = $((Get-date).AddMonths(1)) 
  29. ) 
  30.  
  31. Write-Verbose "Returning objects between: $($start.tostring()) and $($end.tostring())" 
  32. # Load Outlook interop and Outlook iteslf 
  33. [Reflection.Assembly]::LoadWithPartialname("Microsoft.Office.Interop.Outlook") | out-null 
  34. $Outlook = new-object -comobject outlook.application 
  35.  
  36. # Get OL default folders 
  37. $OlFolders = "Microsoft.Office.Interop.Outlook.OlDefaultFolders" -as [type] 
  38. $Namespace = $Outlook.GetNameSpace("MAPI") 
  39. $Calendar = $namespace.GetDefaultFolder($olFolders::olFolderCalendar) 
  40. Write-Verbose "There are $($calendar.items.count) items in the calender in total" 
  41.  
  42. # Now return psobjects for all items between 2 dates 
  43. ForEach ($citem in ($Calendar.Items | sort start)) { 
  44. #Write-Verbose "Processing [$($citem.Subject)]  Starting: [$($Citem.Start)]" 
  45.  
  46. If ($citem.start -ge $start -and $citem.start -LE $end) {  
  47.  
  48. $CalHT =[ordered]  @{ 
  49. Subject          =  $($Citem.Subject) 
  50. Location         =  $($Citem.Location); 
  51. Start            =  $(Get-Date $Citem.Start); 
  52. StartUTC         =  $(Get-Date $Citem.StartUTC);                                   
  53. End              =  $(Get-Date $Citem.End); 
  54. EndUTC           =  $(Get-Date $Citem.EndUTC); 
  55. Duration         =  $($Citem.Duration); 
  56. Importance       =  $($Citem.Importance); 
  57. IsRecurring      =  $($Citem.IsRecurring); 
  58. AllDayEvent      =  $($citem.AllDayEvent); 
  59. Sensitivity      =  $($Citem.Sensitivity); 
  60. ReminderSet      =  $($Citem.ReminderSet); 
  61. CreationTime     =  $($Citem.CreationTime); 
  62. LastModificationTime = $($Citem.LastModificationTime); 
  63. Body             =  $($Citem.Body); 
  64. } 
  65.  
  66.  
  67. # Write the item out as a custom item 
  68. $o=New-Object PSobject -Property $CalHT 
  69. Write-Output $o 
  70.  
  71. } 
  72. } #End of foreach 
  73.  
  74. }  # End of function 
  75.  
  76. Set-Alias GCALI Get-OLCalendarItem  
Technorati Tags: ,

No comments: