PoshCode Archive  Artifact [55c92e6470]

Artifact 55c92e6470e0c523d59e44f861a1757a3dafbd4f98c42e7d96e29e8241bc1c7a:

  • File New-Task.ps1 — part of check-in [5cec33524d] at 2018-06-10 14:24:32 on branch trunk — Allows for the creation of tasks in Microsoft Outlook from Windows PowerShell. The majority of task options available can be configured with the function. (user: Mark E size: 5388)

# encoding: ascii
# api: powershell
# title: New-Task
# description: Allows for the creation of tasks in Microsoft Outlook from Windows PowerShell. The majority of task options available can be configured with the function.
# version: 1.0
# type: script
# author: Mark E
# license: CC0
# x-poshcode-id: 829
# x-archived: 2009-10-25T10:44:45
#
#
<#
.SYNOPSIS
Creates a task in Outlook
.DESCRIPTION
Allows for the creation of tasks in Microsoft Outlook from Windows PowerShell. The majority of task options available can be configured with the function.
.LINK
http://www.leeholmes.com/blog/GettingThingsDoneOutlookTaskAutomationWithPowerShell.aspx
.EXAMPLE
	C:\PS>New-Task -Subject "Create PowerShell script to create Outlook tasks"
	
	Description
	-----------
	This example just a simple Outlook Task with the specified subject.
 .EXAMPLE
  C:\PS>New-Task -Subject "Create powershell script to create outlook calendar entries" -Categories "%PowerShell" -DueDate "2/01/2009" -Status "InProgress" -Display
  
  Description
  -----------
  This example demonstrates the creation of task using additional options. The '-display' parameter brings up the Outlook task form at the end to allow you make additional changes in the UI.
.INPUTTYPE
.RETURNVALUE
.COMPONENT
.ROLE
.FUNCTIONALITY
.PARAMETER Subject
  Sets the subject of the task.
.PARAMETER Body
  Sets the body of the task.
.PARAMETER Categories
  Sets the categories for the task. Comma seperate the values for multiple categories.
.PARAMETER StartDate
  Sets the start date of the task. Must be in a format that can be converted to [datetime] object.
.PARAMETER DueDate
  Sets the due date of the task. Must be in a format that can be converted to [datetime] object.
.PARAMETER PercentComplete
  Sets the percent complete for the new task. Value must be an int from 0 to 100.
.PARAMETER ReminderTime
  Sets the reminder time of the task. Must be in a format that can be converted to [datetime] object.
.PARAMETER Status
  Sets the the status of the new task. Valid values are "Completed", "Deferred", "InProgress","Waiting"
.PARAMETER Priority
  Sets the priority value of the new task. Valid values are "Low", "Medium", "High"
.PARAMETER Display
	Displays the Outlook task form after saving.
.NOTES
Name: New-Task
Author: Mark E. Schill
Date Created: 01/24/2009
Date Revised: 01/24/2009
Version: 1.0
History: 1.0 01/24/2009 - Initial Revision
#>
[CmdletBinding()]
# TODO: Handle the status and priority
param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
[string]
$Subject
,
[Parameter(Mandatory=$false, ValueFromPipeline=$false)]
[string]
$Body
,
# TODO: Be able to specify "today, tomorrow, next week, etc" and convert that to appropriate date.
# http://refactormycode.com/codes/488-parse-relative-date
[Parameter(Mandatory=$false, ValueFromPipeline=$false)]
[DateTime]
$StartDate
,
# TODO: Be able to specify "today, tomorrow, next week, etc" and convert that to appropriate date.
[Parameter(Mandatory=$false, ValueFromPipeline=$false)]
[DateTime]
$DueDate
,
[Parameter(Mandatory=$false, ValueFromPipeline=$false)]
[string]
$Status
,
[Parameter(Mandatory=$false, ValueFromPipeline=$false)]
[string]
$Priority
,
[Parameter(Mandatory=$false, ValueFromPipeline=$false)]
[int]
$PercentComplete = 0
,
[Parameter(Mandatory=$false, ValueFromPipeline=$false)]
[DateTime]
$ReminderTime
,
[Parameter(Mandatory=$false, ValueFromPipeline=$false)]
[string]
$Categories
,
[Parameter(Mandatory=$false, ValueFromPipeline=$false)]
[switch]
$Display
)
Begin
{
	$olTaskItem = "olTaskItem" <# 3 #>
	$olFolderTasks = "olFolderTasks" <# 13 #>
	$Outlook = New-Object -ComObject Outlook.Application
}
Process
{
	$Task = $Outlook.Application.CreateItem($olTaskItem)
	$ContainsError = $false

	$Task.Subject = $Subject
	$Task.PercentComplete = $PercentComplete
	$Task.Body = $Body
	if ( $Categories )  { $Task.Categories = $Categories }  
 	if ( $StartDate ) { $Task.StartDate = $StartDate }
  if ( $DueDate ) { $Task.DueDate   = $DueDate }
	 
  switch ( $Status )
    {
      "Completed" 
        { 
          $Task.Status = [Microsoft.Office.Interop.Outlook.OlTaskStatus]::olTaskComplete 
          $Task.PercentComplete = 100 # Overrides PercentComplete if specified
        }
       "Deferred" { $Task.Status = [Microsoft.Office.Interop.Outlook.OlTaskStatus]::olTaskDeferred }
       "InProgress" { $Task.Status = [Microsoft.Office.Interop.Outlook.OlTaskStatus]::olTaskInProgress }
       "Waiting" { $Task.Status = [Microsoft.Office.Interop.Outlook.OlTaskStatus]::olTaskWaiting }
       default { $Task.Status = [Microsoft.Office.Interop.Outlook.OlTaskStatus]::TaskNotStarted }
    }
  
  switch ($Priority )
    {
       "Low"  { $Task.Importance = [Microsoft.Office.Interop.Outlook.OlImportance]::olImportanceLow }
       "High" { $Task.Importance = [Microsoft.Office.Interop.Outlook.OlImportance]::olImportanceHigh }
       default { $Task.Importance = [Microsoft.Office.Interop.Outlook.OlImportance]::olImportanceNormal }
    }
  
  
	if ( $ReminderTime )
		{
		$Task.ReminderTime = $ReminderTime
		$Task.ReminderSet = $True
		}
  	
  if ( -not $ContainsError)
    { 
		$Task.Save()
		if ($Display ) { $Task.Display() } 
    }
  
}
End
{
  $Outlook = $Null
}