Skip to content

Commit

Permalink
new change task, #103
Browse files Browse the repository at this point in the history
  • Loading branch information
gdbarron committed Jul 14, 2022
1 parent 3898d39 commit aed53a0
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 1 deletion.
130 changes: 130 additions & 0 deletions ServiceNow/Public/New-ServiceNowChangeTask.ps1
Original file line number Diff line number Diff line change
@@ -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 {}
}
2 changes: 1 addition & 1 deletion ServiceNow/ServiceNow.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -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 = @()
Expand Down

0 comments on commit aed53a0

Please sign in to comment.