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. Write-Verbose "Returning objects between: $($start.tostring()) and $($end.tostring())" 
  31. # Load Outlook interop and Outlook iteslf 
  32. [Reflection.Assembly]::LoadWithPartialname("Microsoft.Office.Interop.Outlook") | out-null 
  33. $Outlook = new-object -comobject outlook.application 
  34.  
  35. # Get OL default folders 
  36. $OlFolders = "Microsoft.Office.Interop.Outlook.OlDefaultFolders" -as [type] 
  37. $Namespace = $Outlook.GetNameSpace("MAPI"
  38. $Calendar = $namespace.GetDefaultFolder($olFolders::olFolderCalendar) 
  39. Write-Verbose "There are $($calendar.items.count) items in the calender in total" 
  40.  
  41. # Now return psobjects for all items between 2 dates 
  42. ForEach ($citem in ($Calendar.Items | sort start)) { 
  43. #Write-Verbose "Processing [$($citem.Subject)]  Starting: [$($Citem.Start)]" 
  44.  
  45. If ($citem.start -ge $start -and $citem.start -LE $end) {  
  46.  
  47. $CalHT =[ordered]  @{ 
  48. Subject          =  $($Citem.Subject) 
  49. Location         =  $($Citem.Location); 
  50. Start            =  $(Get-Date $Citem.Start); 
  51. StartUTC         =  $(Get-Date $Citem.StartUTC);                                   
  52. End              =  $(Get-Date $Citem.End); 
  53. EndUTC           =  $(Get-Date $Citem.EndUTC); 
  54. Duration         =  $($Citem.Duration); 
  55. Importance       =  $($Citem.Importance); 
  56. IsRecurring      =  $($Citem.IsRecurring); 
  57. AllDayEvent      =  $($citem.AllDayEvent); 
  58. Sensitivity      =  $($Citem.Sensitivity); 
  59. ReminderSet      =  $($Citem.ReminderSet); 
  60. CreationTime     =  $($Citem.CreationTime); 
  61. LastModificationTime = $($Citem.LastModificationTime); 
  62. Body             =  $($Citem.Body); 
  63.  
  64.  
  65. # Write the item out as a custom item 
  66. $o=New-Object PSobject -Property $CalHT 
  67. Write-Output $o 
  68.  
  69. } #End of foreach 
  70.  
  71. }  # End of function 
  72.  
  73. Set-Alias GCALI Get-OLCalendarItem  
Technorati Tags: ,

No comments: