Skip to content

Commit 3aea381

Browse files
committed
start of new ci
1 parent 8fd6b23 commit 3aea381

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<#
2+
.SYNOPSIS
3+
Generates a new configuration item
4+
5+
.DESCRIPTION
6+
Generates a new ServiceNow Incident using predefined or custom fields by invoking the ServiceNow API
7+
8+
.EXAMPLE
9+
Generate a basic Incident attributed to the caller "UserName" with descriptions, categories, assignment groups and CMDB items set.
10+
New-ServiceNowIncident -Caller "UserName" -ShortDescription = "New PS Incident" -Description = "This incident was created from Powershell" -AssignmentGroup "ServiceDesk" -Comment "Inline Comment" -Category "Office" -Subcategory "Outlook" -ConfigurationItem UserPC1
11+
12+
.EXAMPLE
13+
Generate an Incident by "Splatting" all fields used in the 1st example plus some additional custom ServiceNow fields (These must exist in your ServiceNow Instance):
14+
15+
$IncidentParams = @{Caller = "UserName"
16+
ShortDescription = "New PS Incident"
17+
Description = "This incident was created from Powershell"
18+
AssignmentGroup "ServiceDesk"
19+
Comment "Inline Comment"
20+
Category "Office"
21+
Subcategory "Outlook"
22+
ConfigurationItem UserPC1
23+
CustomFields = @{u_custom1 = "Custom Field Entry"
24+
u_another_custom = "Related Test"}
25+
}
26+
New-ServiceNowIncident @Params
27+
28+
#>
29+
function New-ServiceNowConfigurationItem {
30+
31+
[CmdletBinding(DefaultParameterSetName = 'Session', SupportsShouldProcess)]
32+
33+
Param(
34+
35+
# sys_id of the caller of the incident (user Get-ServiceNowUser to retrieve this)
36+
[parameter(Mandatory)]
37+
[string] $Name,
38+
39+
# Short description of the incident
40+
[parameter(Mandatory)]
41+
[string] $ShortDescription,
42+
43+
# Long description of the incident
44+
[parameter()]
45+
[string] $Description,
46+
47+
# sys_id of the assignment group (use Get-ServiceNowUserGroup to retrieve this)
48+
[parameter()]
49+
[string] $AssignmentGroup,
50+
51+
# Comment to include in the ticket
52+
[parameter()]
53+
[string] $Comment,
54+
55+
# Category of the incident (e.g. 'Network')
56+
[parameter()]
57+
[string] $Category,
58+
59+
# Subcategory of the incident (e.g. 'Network')
60+
[parameter()]
61+
[string] $Subcategory,
62+
63+
# sys_id of the configuration item of the incident
64+
[parameter()]
65+
[string] $ConfigurationItem,
66+
67+
# custom fields as hashtable
68+
[parameter()]
69+
[hashtable] $CustomFields,
70+
71+
#Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
72+
[Parameter(ParameterSetName = 'UseConnectionObject', Mandatory)]
73+
[ValidateNotNullOrEmpty()]
74+
[Hashtable] $Connection,
75+
76+
[Parameter(ParameterSetName = 'Session')]
77+
[ValidateNotNullOrEmpty()]
78+
[hashtable] $ServiceNowSession = $script:ServiceNowSession,
79+
80+
[Parameter()]
81+
[switch] $PassThru
82+
)
83+
84+
begin {}
85+
86+
process {
87+
# Create a hash table of any defined parameters (not CustomFields) that have values
88+
# $DefinedIncidentParameters = @('AssignmentGroup', 'Caller', 'Category', 'Comment', 'ConfigurationItem', 'Description', 'ShortDescription', 'Subcategory')
89+
$definedParams = @{
90+
'Name' = 'name'
91+
}
92+
$tableEntryValues = @{}
93+
foreach ($key in $PSBoundParameters.Keys) {
94+
if ($definedParams.$key) {
95+
$tableEntryValues.Add($definedParams.$key, $PSBoundParameters.$key)
96+
}
97+
}
98+
# ForEach ($Parameter in $DefinedIncidentParameters) {
99+
# If ($null -ne $PSBoundParameters.$Parameter) {
100+
# # Turn the defined parameter name into the ServiceNow attribute name
101+
# $KeyToAdd = Switch ($Parameter) {
102+
# AssignmentGroup { 'assignment_group'; break }
103+
# Caller { 'caller_id'; break }
104+
# Category { 'category'; break }
105+
# Comment { 'comments'; break }
106+
# ConfigurationItem { 'cmdb_ci'; break }
107+
# Description { 'description'; break }
108+
# ShortDescription { 'short_description'; break }
109+
# Subcategory { 'subcategory'; break }
110+
# }
111+
# $TableEntryValues.Add($KeyToAdd, $PSBoundParameters.$Parameter)
112+
# }
113+
# }
114+
115+
# Add CustomFields hash pairs to the Table Entry Values hash table
116+
$dupes = ForEach ($Key in $CustomFields.Keys) {
117+
If ($TableEntryValues.ContainsKey($Key)) {
118+
# Capture the duplicate key name
119+
$Key
120+
}
121+
Else {
122+
# Add the unique entry to the table entry values hash table
123+
$TableEntryValues.Add($Key, $CustomFields[$Key])
124+
}
125+
}
126+
127+
# Throw an error if duplicate fields were provided
128+
If ($dupes) {
129+
throw ('Ticket fields may only be used once and you have redefined ''{0}'' in $CustomFields' -f ($dupes -join ","))
130+
}
131+
132+
# Table Entry Splat
133+
$params = @{
134+
Table = 'cmdb_ci'
135+
Values = $TableEntryValues
136+
Connection = $Connection
137+
ServiceNowSession = $ServiceNowSession
138+
PassThru = $true
139+
}
140+
141+
If ( $PSCmdlet.ShouldProcess($Name, 'Create new configuration item') ) {
142+
$response = New-ServiceNowRecord @params
143+
# $response = Invoke-ServiceNowRestMethod @params
144+
If ($PassThru.IsPresent) {
145+
$response.PSObject.TypeNames.Insert(0, "ServiceNow.ConfigurationItem")
146+
$response
147+
}
148+
}
149+
}
150+
}

0 commit comments

Comments
 (0)