Skip to content

Commit bc2eb2c

Browse files
committed
Cleaned up New-ServiceNowIncident. Added error checking for duplicate incident fields
1 parent e00acb0 commit bc2eb2c

File tree

1 file changed

+63
-36
lines changed

1 file changed

+63
-36
lines changed

ServiceNow/Public/New-ServiceNowIncident.ps1

Lines changed: 63 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ function New-ServiceNowIncident{
22
Param(
33

44
# sys_id of the caller of the incident (user Get-ServiceNowUser to retrieve this)
5-
[parameter(ParameterSetName='SpecifyConnectionFields', mandatory=$true)]
6-
[parameter(ParameterSetName='UseConnectionObject', mandatory=$true)]
7-
[parameter(ParameterSetName='SetGlobalAuth', mandatory=$true)]
5+
[parameter(Mandatory=$true)]
6+
[parameter(ParameterSetName='SpecifyConnectionFields')]
7+
[parameter(ParameterSetName='UseConnectionObject')]
8+
[parameter(ParameterSetName='SetGlobalAuth')]
89
[string]$Caller,
910

1011
# Short description of the incident
11-
[parameter(mandatory=$true)]
12-
[parameter(ParameterSetName='SpecifyConnectionFields', mandatory=$true)]
13-
[parameter(ParameterSetName='UseConnectionObject', mandatory=$true)]
14-
[parameter(ParameterSetName='SetGlobalAuth', mandatory=$true)]
12+
[parameter(Mandatory=$true)]
13+
[parameter(ParameterSetName='SpecifyConnectionFields')]
14+
[parameter(ParameterSetName='UseConnectionObject')]
15+
[parameter(ParameterSetName='SetGlobalAuth')]
1516
[string]$ShortDescription,
1617

1718
# Long description of the incident
@@ -66,50 +67,76 @@ function New-ServiceNowIncident{
6667
# Credential used to authenticate to ServiceNow
6768
[Parameter(ParameterSetName='SpecifyConnectionFields', Mandatory=$True)]
6869
[ValidateNotNullOrEmpty()]
69-
[PSCredential]
70-
$ServiceNowCredential,
70+
[PSCredential]$ServiceNowCredential,
7171

7272
# The URL for the ServiceNow instance being used (eg: instancename.service-now.com)
7373
[Parameter(ParameterSetName='SpecifyConnectionFields', Mandatory=$True)]
7474
[ValidateNotNullOrEmpty()]
75-
[string]
76-
$ServiceNowURL,
75+
[string]$ServiceNowURL,
7776

7877
#Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
7978
[Parameter(ParameterSetName='UseConnectionObject', Mandatory=$True)]
8079
[ValidateNotNullOrEmpty()]
81-
[Hashtable]
82-
$Connection
80+
[Hashtable]$Connection
8381
)
8482

85-
86-
$Values = @{
87-
'caller_id' = $Caller
88-
'short_description' = $ShortDescription
89-
'description' = $Description
90-
'assignment_group' = $AssignmentGroup
91-
'comments' = $Comment
92-
'category' = $Category
93-
'subcategory' = $Subcategory
94-
'cmdb_ci' = $ConfigurationItem
83+
# Create a hash table of any defined parameters (not CustomFields) that have values
84+
$DefinedIncidentParameters = @('AssignmentGroup','Caller','Category','Comment','ConfigurationItem','Description','ShortDescription','Subcategory')
85+
$TableEntryValues = @{}
86+
ForEach ($Parameter in $DefinedIncidentParameters) {
87+
If ($null -ne $PSBoundParameters.$Parameter) {
88+
# Turn the defined parameter name into the ServiceNow attribute name
89+
$KeyToAdd = Switch ($Parameter) {
90+
AssignmentGroup {'assignment_group'}
91+
Caller {'caller_id'}
92+
Category {'category'}
93+
Comment {'comments'}
94+
ConfigurationItem {'cmdb_ci'}
95+
Description {'description'}
96+
ShortDescription {'short_description'}
97+
Subcategory {'subcategory'}
98+
}
99+
$TableEntryValues.Add($KeyToAdd,$PSBoundParameters.$Parameter)
100+
}
95101
}
96102

97-
if($CustomFields)
98-
{
99-
$Values += $CustomFields
103+
# Add CustomFields hash pairs to the Table Entry Values hash table
104+
If ($null -ne $PSBoundParameters.CustomFields) {
105+
$DuplicateTableEntryValues = ForEach ($Key in $CustomFields.Keys) {
106+
If (($TableEntryValues.ContainsKey($Key) -eq $False)) {
107+
# Add the unique entry to the table entry values hash table
108+
$TableEntryValues.Add($Key,$CustomFields[$Key])
109+
}
110+
Else {
111+
# Capture the duplicate key name
112+
$Key
113+
}
114+
}
115+
}
116+
117+
# Throw an error if duplicate fields were provided
118+
If ($null -ne $DuplicateTableEntryValues) {
119+
$DuplicateKeyList = $DuplicateTableEntryValues -join ","
120+
Throw "Ticket fields may only be used once: $DuplicateKeyList"
100121
}
101122

102-
if ($Connection -ne $null)
103-
{
104-
New-ServiceNowTableEntry -Table 'incident' -Values $Values -Connection $Connection
123+
# Table Entry Splat
124+
$newServiceNowTableEntrySplat = @{
125+
Table = 'incident'
126+
Values = $TableEntryValues
105127
}
106-
elseif ($ServiceNowCredential -ne $null -and $ServiceNowURL -ne $null)
107-
{
108-
New-ServiceNowTableEntry -Table 'incident' -Values $Values -ServiceNowCredential $ServiceNowCredential -ServiceNowURL $ServiceNowURL
128+
129+
# Update the splat if the parameters have values
130+
if ($null -ne $PSBoundParameters.Connection)
131+
{
132+
$newServiceNowTableEntrySplat.Add('Connection',$Connection)
109133
}
110-
else
134+
elseif ($null -ne $PSBoundParameters.ServiceNowCredential -and $null -ne $PSBoundParameters.ServiceNowURL)
111135
{
112-
New-ServiceNowTableEntry -Table 'incident' -Values $Values
113-
}
114-
136+
$newServiceNowTableEntrySplat.Add('ServiceNowCredential',$ServiceNowCredential)
137+
$newServiceNowTableEntrySplat.Add('ServiceNowURL',$ServiceNowURL)
138+
}
139+
140+
# Create the table entry
141+
New-ServiceNowTableEntry @newServiceNowTableEntrySplat
115142
}

0 commit comments

Comments
 (0)