From aed53a0589c5566859f3f949e3644aac616b990c Mon Sep 17 00:00:00 2001 From: Greg Brownstein Date: Thu, 14 Jul 2022 09:47:01 -0400 Subject: [PATCH] new change task, #103 --- .../Public/New-ServiceNowChangeTask.ps1 | 130 ++++++++++++++++++ ServiceNow/ServiceNow.psd1 | 2 +- 2 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 ServiceNow/Public/New-ServiceNowChangeTask.ps1 diff --git a/ServiceNow/Public/New-ServiceNowChangeTask.ps1 b/ServiceNow/Public/New-ServiceNowChangeTask.ps1 new file mode 100644 index 0000000..a879cd5 --- /dev/null +++ b/ServiceNow/Public/New-ServiceNowChangeTask.ps1 @@ -0,0 +1,130 @@ +function New-ServiceNowChangeTask { + <# + .SYNOPSIS + Create a new change task + + .DESCRIPTION + Create a new change task + + .PARAMETER ChangeRequest + sys_id or number of parent change request + + .PARAMETER ShortDescription + Short description of the change task + + .PARAMETER Description + Long description of the change task + + .PARAMETER AssignmentGroup + sys_id or name of the assignment group + + .PARAMETER AssignedTo + sys_id or name of the assigned user + + .PARAMETER CustomField + Other key/value pairs to create the task which are not one of the existing parameters + + .PARAMETER Connection + Azure Automation Connection object containing username, password, and URL for the ServiceNow instance + + .PARAMETER PassThru + Return the newly created item details + + .EXAMPLE + New-ServiceNowChangeTask -ChangeRequest RITM0010001 -ShortDescription 'New PS change request' -Description 'This change request was created from Powershell' -AssignmentGroup ServiceDesk + + Create a new task + + .EXAMPLE + New-ServiceNowChangeTask -ShortDescription 'New' -Description 'Longer description' -CustomField @{'impact'='3'} + + Create a new task with additional fields + #> + + [CmdletBinding(SupportsShouldProcess)] + + Param( + + [Parameter(Mandatory)] + [string] $ShortDescription, + + [parameter(Mandatory)] + [string] $Description, + + [Parameter()] + [string] $ChangeRequest, + + [Parameter()] + [string] $AssignmentGroup, + + [Parameter()] + [string] $AssignedTo, + + [Parameter()] + [hashtable] $CustomField, + + [Parameter()] + [Hashtable] $Connection, + + [Parameter()] + [hashtable] $ServiceNowSession = $script:ServiceNowSession, + + [Parameter()] + [switch] $PassThru + ) + + begin { + $createValues = @{} + } + + process { + + switch ($PSBoundParameters.Keys) { + 'ChangeRequest' { + $createValues.parent = $ChangeRequest + } + + 'ShortDescription' { + $createValues.short_description = $ShortDescription + } + + 'Description' { + $createValues.description = $Description + } + + 'AssignmentGroup' { + $createValues.assignment_group = $AssignmentGroup + } + + 'AssignedTo' { + $createValues.assignment_to = $AssignmentTo + } + } + + $CustomField.GetEnumerator() | ForEach-Object { + if ( $createValues.($_.Key) ) { + Write-Warning ('Custom field {0} has already been added via parameter' -f $_.Key) + } else { + $createValues.Add($_.Key, $_.Value) + } + } + + $params = @{ + Method = 'Post' + Table = 'change_task' + Values = $createValues + Connection = $Connection + ServiceNowSession = $ServiceNowSession + } + + If ( $PSCmdlet.ShouldProcess($ShortDescription, 'Create new change task') ) { + $response = Invoke-ServiceNowRestMethod @params + If ( $PassThru ) { + $response.PSObject.TypeNames.Insert(0, "ServiceNow.ChangeTask") + $response + } + } + } + + end {} +} diff --git a/ServiceNow/ServiceNow.psd1 b/ServiceNow/ServiceNow.psd1 index 8d0ec20..0975641 100644 --- a/ServiceNow/ServiceNow.psd1 +++ b/ServiceNow/ServiceNow.psd1 @@ -82,7 +82,7 @@ FunctionsToExport = 'Get-ServiceNowRecordInterim', 'New-ServiceNowConfigurationI 'Remove-ServiceNowAttachment', 'Remove-ServiceNowRecord', 'Update-ServiceNowChangeRequest', 'Update-ServiceNowIncident', 'Update-ServiceNowRequestedItem', 'Update-ServiceNowRecord', - 'Export-ServiceNowRecord' + 'Export-ServiceNowRecord', 'Invoke-ServiceNowGraphQL', 'New-ServiceNowChangeTask' # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. CmdletsToExport = @()