forked from Snow-Shell/servicenow-powershell
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPSServiceNow-Incidents.psm1
227 lines (195 loc) · 9.21 KB
/
PSServiceNow-Incidents.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<#
.Synopsis
Returns incidents from the connected ServiceNow instance based (optionally based on criteria)
.NOTES
You must have invoked Set-ServiceNowAuth prior to executing this cmdlet
.EXAMPLE
Return the incident whose number is exactly INC0010683
Get-ServiceNowIncident -MatchExact @{number='INC0010683'}
.EXAMPLE
Return all incidents where the short description contains the word 'user'
Get-ServiceNowIncident -MatchContains @{short_description='user'}
#>
function Get-ServiceNowIncident{
param(
# Machine name of the field to order by
[parameter(mandatory=$false)]
[parameter(ParameterSetName='SpecifyConnectionFields')]
[parameter(ParameterSetName='UseConnectionObject')]
[parameter(ParameterSetName='SetGlobalAuth')]
[string]$OrderBy='opened_at',
# Direction of ordering (Desc/Asc)
[parameter(mandatory=$false)]
[parameter(ParameterSetName='SpecifyConnectionFields')]
[parameter(ParameterSetName='UseConnectionObject')]
[parameter(ParameterSetName='SetGlobalAuth')]
[ValidateSet("Desc", "Asc")]
[string]$OrderDirection='Desc',
# Maximum number of records to return
[parameter(mandatory=$false)]
[parameter(ParameterSetName='SpecifyConnectionFields')]
[parameter(ParameterSetName='UseConnectionObject')]
[parameter(ParameterSetName='SetGlobalAuth')]
[int]$Limit=10,
# Hashtable containing machine field names and values returned must match exactly (will be combined with AND)
[parameter(mandatory=$false)]
[parameter(ParameterSetName='SpecifyConnectionFields')]
[parameter(ParameterSetName='UseConnectionObject')]
[parameter(ParameterSetName='SetGlobalAuth')]
[hashtable]$MatchExact=@{},
# Hashtable containing machine field names and values returned rows must contain (will be combined with AND)
[parameter(mandatory=$false)]
[parameter(ParameterSetName='SpecifyConnectionFields')]
[parameter(ParameterSetName='UseConnectionObject')]
[parameter(ParameterSetName='SetGlobalAuth')]
[hashtable]$MatchContains=@{},
# Whether or not to show human readable display values instead of machine values
[parameter(mandatory=$false)]
[parameter(ParameterSetName='SpecifyConnectionFields')]
[parameter(ParameterSetName='UseConnectionObject')]
[parameter(ParameterSetName='SetGlobalAuth')]
[ValidateSet("true","false", "all")]
[string]$DisplayValues='true',
# Credential used to authenticate to ServiceNow
[Parameter(ParameterSetName='SpecifyConnectionFields', Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[PSCredential]
$ServiceNowCredential,
# The URL for the ServiceNow instance being used
[Parameter(ParameterSetName='SpecifyConnectionFields', Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[string]
$ServiceNowURL,
#Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
[Parameter(ParameterSetName='UseConnectionObject', Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[Hashtable]
$Connection
)
$Query = New-ServiceNowQuery -OrderBy $OrderBy -OrderDirection $OrderDirection -MatchExact $MatchExact -MatchContains $MatchContains
if ($Connection -ne $null)
{
$result = Get-ServiceNowTable -Table 'incident' -Query $Query -Limit $Limit -DisplayValues $DisplayValues -Connection $Connection
}
elseif ($ServiceNowCredential -ne $null -and $ServiceNowURL -ne $null)
{
$result = Get-ServiceNowTable -Table 'incident' -Query $Query -Limit $Limit -DisplayValues $DisplayValues -ServiceNowCredential $ServiceNowCredential -ServiceNowURL $ServiceNowURL
}
else
{
$result = Get-ServiceNowTable -Table 'incident' -Query $Query -Limit $Limit -DisplayValues $DisplayValues
}
# Set the default property set for the table view
$DefaultProperties = @('number', 'short_description', 'opened_at')
$DefaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet(‘DefaultDisplayPropertySet’,[string[]]$DefaultProperties)
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($DefaultDisplayPropertySet)
$Result | Add-Member MemberSet PSStandardMembers $PSStandardMembers
# Return that result!
return $result
}
<#
.EXAMPLE
New-ServiceNowIncident -ShortDescription "Testing with Pester" `
-Description "Long description" -AssignmentGroup "e9e9a2406f4c35001855fa0dba3ee4f3" `
-Category "Internal" -SubCategory "Task" `
-Comment "Comment" -ConfigurationItem "bee8e0ed6f8475001855fa0dba3ee4ea" `
-Caller "7a4b573a6f3725001855fa0dba3ee485" `
#>
function New-ServiceNowIncident{
Param(
# sys_id of the caller of the incident (user Get-ServiceNowUser to retrieve this)
[parameter(ParameterSetName='SpecifyConnectionFields', mandatory=$true)]
[parameter(ParameterSetName='UseConnectionObject', mandatory=$true)]
[parameter(ParameterSetName='SetGlobalAuth', mandatory=$true)]
[string]$Caller,
# Short description of the incident
[parameter(mandatory=$true)]
[parameter(ParameterSetName='SpecifyConnectionFields', mandatory=$true)]
[parameter(ParameterSetName='UseConnectionObject', mandatory=$true)]
[parameter(ParameterSetName='SetGlobalAuth', mandatory=$true)]
[string]$ShortDescription,
# Long description of the incident
[parameter(mandatory=$false)]
[parameter(ParameterSetName='SpecifyConnectionFields')]
[parameter(ParameterSetName='UseConnectionObject')]
[parameter(ParameterSetName='SetGlobalAuth')]
[string]$Description,
# sys_id of the assignment group (use Get-ServiceNowUserGroup to retrieve this)
[parameter(mandatory=$false)]
[parameter(ParameterSetName='SpecifyConnectionFields')]
[parameter(ParameterSetName='UseConnectionObject')]
[parameter(ParameterSetName='SetGlobalAuth')]
[string]$AssignmentGroup,
# Comment to include in the ticket
[parameter(mandatory=$false)]
[parameter(ParameterSetName='SpecifyConnectionFields')]
[parameter(ParameterSetName='UseConnectionObject')]
[parameter(ParameterSetName='SetGlobalAuth')]
[string]$Comment,
# Category of the incident (e.g. 'Network')
[parameter(mandatory=$false)]
[parameter(ParameterSetName='SpecifyConnectionFields')]
[parameter(ParameterSetName='UseConnectionObject')]
[parameter(ParameterSetName='SetGlobalAuth')]
[string]$Category,
# Subcategory of the incident (e.g. 'Network')
[parameter(mandatory=$false)]
[parameter(ParameterSetName='SpecifyConnectionFields')]
[parameter(ParameterSetName='UseConnectionObject')]
[parameter(ParameterSetName='SetGlobalAuth')]
[string]$Subcategory,
# sys_id of the configuration item of the incident
[parameter(mandatory=$false)]
[parameter(ParameterSetName='SpecifyConnectionFields')]
[parameter(ParameterSetName='UseConnectionObject')]
[parameter(ParameterSetName='SetGlobalAuth')]
[string]$ConfigurationItem,
# custom fields as hashtable
[parameter(mandatory=$false)]
[parameter(ParameterSetName='SpecifyConnectionFields')]
[parameter(ParameterSetName='UseConnectionObject')]
[parameter(ParameterSetName='SetGlobalAuth')]
[hashtable]$CustomFields,
# Credential used to authenticate to ServiceNow
[Parameter(ParameterSetName='SpecifyConnectionFields', Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[PSCredential]
$ServiceNowCredential,
# The URL for the ServiceNow instance being used (eg: instancename.service-now.com)
[Parameter(ParameterSetName='SpecifyConnectionFields', Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[string]
$ServiceNowURL,
#Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
[Parameter(ParameterSetName='UseConnectionObject', Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[Hashtable]
$Connection
)
$Values = @{
'caller_id' = $Caller
'short_description' = $ShortDescription
'description' = $Description
'assignment_group' = $AssignmentGroup
'comments' = $Comment
'category' = $Category
'subcategory' = $Subcategory
'cmdb_ci' = $ConfigurationItem
}
if($CustomFields)
{
$Values += $CustomFields
}
if ($Connection -ne $null)
{
New-ServiceNowTableEntry -Table 'incident' -Values $Values -Connection $Connection
}
elseif ($ServiceNowCredential -ne $null -and $ServiceNowURL -ne $null)
{
New-ServiceNowTableEntry -Table 'incident' -Values $Values -ServiceNowCredential $ServiceNowCredential -ServiceNowURL $ServiceNowURL
}
else
{
New-ServiceNowTableEntry -Table 'incident' -Values $Values
}
}