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