Files
Zack Meier 1d304511b8 update
2026-04-15 15:45:50 -05:00

120 lines
3.8 KiB
PowerShell

<#
.SYNOPSIS
Creates a new incident in ServiceNow.
.DESCRIPTION
This function creates a new incident in ServiceNow using the provided parameters. It requires either the caller's username or email, along with a short description and a detailed description of the incident.
.PARAMETER CallerUsername
The username of the caller reporting the incident. This parameter is mandatory if CallerEmail is not provided.
.PARAMETER CallerEmail
The email address of the caller reporting the incident. This parameter is mandatory if CallerUsername is not provided.
.PARAMETER ShortDescription
A brief summary of the incident.
.PARAMETER Description
A detailed description of the incident.
.PARAMETER Impact
The impact level of the incident. Valid values are 1 (High), 2 (Medium), and 3 (Low). Default is 3.
.PARAMETER Urgency
The urgency level of the incident. Valid values are 1 (High), 2 (Medium), and 3 (Low). Default is 3.
.PARAMETER Category
The category of the incident.
.EXAMPLE
New-ITDServiceNowIncident -CallerUsername 'jdoe' -ShortDescription 'System outage' -Description 'The system is down and not responding.' -Impact 1 -Urgency 1 -Category 'Network'
This example creates a new high-impact, high-urgency incident for a system outage reported by the user 'jdoe'.
#>
function New-ITDServiceNowIncident {
[CmdletBinding()]
param (
[Parameter(ParameterSetName = 'CallerUsername', Mandatory = $true)]
[string]
$CallerUsername,
[Parameter(ParameterSetName = 'CallerEmail', Mandatory = $true)]
[string]
$CallerEmail,
[Parameter(Mandatory = $true)]
[string]
$ShortDescription,
[Parameter(Mandatory = $true)]
[string]
$Description,
[ValidateRange(1, 3)]
[int]
$Impact = 3,
[ValidateRange(1, 3)]
[int]
$Urgency = 3,
[string]
$Category,
[string]
$Subcategory,
[string]
$AssignmentGroup,
[string]
$AssignedTo
)
begin {
}
process {
# determine caller id
switch ($PSCmdlet.ParameterSetName) {
'CallerUsername' {
$Caller = Get-ITDServiceNowUser -Username $CallerUsername
}
'CallerEmail' {
$Caller = Get-ITDServiceNowUser -Email $CallerEmail
}
}
If (@($Caller).count -gt 1) {
Write-Error "Multiple users found, incident creation failed." -ErrorAction Stop
}
[PSCustomObject]$NewRecord = @{
caller_id = $Caller.sys_id
short_description = $ShortDescription;
description = $Description;
impact = $Impact;
urgency = $Urgency;
category = $Category;
subcategory = $Subcategory
}
If ($AssignmentGroup) {
$AssGroup = Get-ITDServiceNowUserGroup -Name $AssignmentGroup
If (@($AssGroup).count -gt 1) {
Write-Error "Multiple assignment groups found, incident creation failed." -ErrorAction Step
}
Else {
$NewRecord += @{assignment_group = $AssGroup.sys_id }
}
}
$InvokeRestMethodParams = @{
Method = 'Post';
Uri = ($Script:ServiceNowSession.Uri + "/api/now/table/incident");
Body = $NewRecord | ConvertTo-Json;
Headers = $Script:ServiceNowSession.Headers;
ContentType = "application/json"
}
#Write-Output $InvokeRestMethodParams
$result = Invoke-RestMethod @InvokeRestMethodParams
}
end {
$result #| select number, impact, urgency, assignment_group, short_description
}
}