@@ -138,8 +138,8 @@ function Get-ServiceNowRecord {
138
138
[Alias (' sys_class_name' )]
139
139
[string ] $Table ,
140
140
141
- [Parameter (ParameterSetName = ' Id' , Mandatory , Position = 0 )]
142
- [Parameter (ParameterSetName = ' Table' )]
141
+ [Parameter (ParameterSetName = ' Id' , Mandatory , ValueFromPipeline , ValueFromPipelineByPropertyName , Position = 0 )]
142
+ [Parameter (ParameterSetName = ' Table' , ValueFromPipeline , ValueFromPipelineByPropertyName )]
143
143
[ValidateScript ( {
144
144
if ($_ -match ' ^[a-zA-Z0-9]{32}$' -or $_ -match ' ^([a-zA-Z]+)[0-9]+$' ) {
145
145
$true
@@ -194,146 +194,154 @@ function Get-ServiceNowRecord {
194
194
[hashtable ] $ServiceNowSession = $script :ServiceNowSession
195
195
)
196
196
197
- $invokeParams = @ {
198
- Filter = $Filter
199
- Property = $Property
200
- Sort = $Sort
201
- DisplayValue = $DisplayValue
202
- First = $PSCmdlet.PagingParameters.First
203
- Skip = $PSCmdlet.PagingParameters.Skip
204
- IncludeTotalCount = $PSCmdlet.PagingParameters.IncludeTotalCount
205
- Connection = $Connection
206
- ServiceNowSession = $ServiceNowSession
207
- }
197
+ begin {
198
+
199
+ $invokeParams = @ {
200
+ Property = $Property
201
+ Sort = $Sort
202
+ DisplayValue = $DisplayValue
203
+ First = $PSCmdlet.PagingParameters.First
204
+ Skip = $PSCmdlet.PagingParameters.Skip
205
+ IncludeTotalCount = $PSCmdlet.PagingParameters.IncludeTotalCount
206
+ Connection = $Connection
207
+ ServiceNowSession = $ServiceNowSession
208
+ }
208
209
209
- if ( $Table ) {
210
- $thisTable = $script :ServiceNowTable | Where-Object { $_.Name.ToLower () -eq $Table.ToLower () -or $_.ClassName.ToLower () -eq $Table.ToLower () }
211
- if ( -not $thisTable ) {
212
- # we aren't aware of this table, create default config
213
- $thisTable = @ {
214
- Name = $Table
215
- ClassName = $null
216
- Type = $null
217
- NumberPrefix = $null
218
- DescriptionField = $null
210
+ if ( $Table ) {
211
+ $thisTable = $script :ServiceNowTable | Where-Object { $_.Name.ToLower () -eq $Table.ToLower () -or $_.ClassName.ToLower () -eq $Table.ToLower () }
212
+ if ( -not $thisTable ) {
213
+ # we aren't aware of this table, create default config
214
+ $thisTable = @ {
215
+ Name = $Table
216
+ ClassName = $null
217
+ Type = $null
218
+ NumberPrefix = $null
219
+ DescriptionField = $null
220
+ }
219
221
}
220
222
}
221
223
}
222
224
223
- if ( $Id ) {
224
- if ( $Id -match ' ^[a-zA-Z0-9]{32}$' ) {
225
- if ( -not $thisTable ) {
226
- throw ' Providing sys_id for -Id requires a value for -Table. Alternatively, provide an Id with a prefix, eg. INC1234567, and the table will be automatically determined.'
227
- }
225
+ process {
228
226
229
- $idFilter = @ (' sys_id' , ' -eq' , $Id )
230
- }
231
- else {
232
- if ( -not $thisTable ) {
233
- # get table name from prefix if only Id was provided
234
- $idPrefix = ($Id | Select-String - Pattern ' ^([a-zA-Z]+)([0-9]+$)' ).Matches.Groups[1 ].Value.ToLower()
235
- Write-Debug " Id prefix is $idPrefix "
236
- $thisTable = $script :ServiceNowTable | Where-Object { $_.NumberPrefix -and $idPrefix -eq $_.NumberPrefix }
227
+ if ( $Id ) {
228
+ if ( $Id -match ' ^[a-zA-Z0-9]{32}$' ) {
237
229
if ( -not $thisTable ) {
238
- throw ( ' The prefix for Id '' {0} '' was not found and the appropriate table cannot be determined . Known prefixes are {1}. Please provide a value for -Table. ' -f $Id , ( $ServiceNowTable .NumberPrefix.Where ( { $_ }) -join ' , ' ))
230
+ throw ' Providing sys_id for - Id requires a value for -Table . Alternatively, provide an Id with a prefix, eg. INC1234567, and the table will be automatically determined. '
239
231
}
232
+
233
+ $idFilter = @ (' sys_id' , ' -eq' , $Id )
234
+ }
235
+ else {
236
+ if ( -not $thisTable ) {
237
+ # get table name from prefix if only Id was provided
238
+ $idPrefix = ($Id | Select-String - Pattern ' ^([a-zA-Z]+)([0-9]+$)' ).Matches.Groups[1 ].Value.ToLower()
239
+ Write-Debug " Id prefix is $idPrefix "
240
+ $thisTable = $script :ServiceNowTable | Where-Object { $_.NumberPrefix -and $idPrefix -eq $_.NumberPrefix }
241
+ if ( -not $thisTable ) {
242
+ throw (' The prefix for Id '' {0}'' was not found and the appropriate table cannot be determined. Known prefixes are {1}. Please provide a value for -Table.' -f $Id , ($ServiceNowTable.NumberPrefix.Where ( { $_ }) -join ' , ' ))
243
+ }
244
+ }
245
+ $idFilter = @ (' number' , ' -eq' , $Id )
246
+ }
247
+
248
+ if ( $Filter ) {
249
+ $invokeParams.Filter = $Filter , ' and' , $idFilter
250
+ }
251
+ else {
252
+ $invokeParams.Filter = $idFilter
240
253
}
241
- $idFilter = @ (' number' , ' -eq' , $Id )
242
- }
243
254
244
- if ( $invokeParams.Filter ) {
245
- $invokeParams.Filter = $invokeParams.Filter , ' and' , $idFilter
246
- }
247
- else {
248
- $invokeParams.Filter = $idFilter
249
255
}
250
256
251
- }
257
+ # we have the table, update the params
258
+ $invokeParams.Table = $thisTable.Name
252
259
253
- # we have the table, update the params
254
- $invokeParams.Table = $thisTable.Name
260
+ if ( $ParentId ) {
261
+ if ( $ParentId -match ' ^[a-zA-Z0-9]{32}$' ) {
262
+ $parentIdFilter = @ (' parent.sys_id' , ' -eq' , $ParentId )
263
+ }
264
+ else {
265
+ $parentIdFilter = @ (' parent.number' , ' -eq' , $ParentId )
266
+ }
255
267
256
- if ( $ParentId ) {
257
- if ( $ParentId -match ' ^[a-zA-Z0-9]{32}$ ' ) {
258
- $parentIdFilter = @ ( ' parent.sys_id ' , ' -eq ' , $ParentId )
259
- }
260
- else {
261
- $parentIdFilter = @ ( ' parent.number ' , ' -eq ' , $ParentId )
268
+ if ( $invokeParams .Filter ) {
269
+ $invokeParams .Filter = $invokeParams .Filter , ' and ' , $parentIdFilter
270
+ }
271
+ else {
272
+ $invokeParams .Filter = $parentIdFilter
273
+ }
262
274
}
263
275
264
- if ( $invokeParams.Filter ) {
265
- $invokeParams.Filter = $invokeParams.Filter , ' and' , $parentIdFilter
266
- }
267
- else {
268
- $invokeParams.Filter = $parentIdFilter
269
- }
270
- }
276
+ if ( $Description ) {
277
+ # determine the field we should compare for 'description' and add the filter
278
+ if ( -not $thisTable.DescriptionField ) {
279
+ Write-Warning (' We do not have table '' {0}'' in the config; short_description will be used as the description field' -f $thisTable.Name )
280
+ $thisTable.DescriptionField = ' short_description'
281
+ }
271
282
272
- if ( $Description ) {
273
- # determine the field we should compare for 'description' and add the filter
274
- if ( -not $thisTable.DescriptionField ) {
275
- Write-Warning (' We do not have table '' {0}'' in the config; short_description will be used as the description field' -f $thisTable.Name )
276
- $thisTable.DescriptionField = ' short_description'
283
+ if ( $invokeParams.Filter ) {
284
+ $invokeParams.Filter = $invokeParams.Filter , ' and' , @ ($thisTable.DescriptionField , ' -like' , $Description )
285
+ }
286
+ else {
287
+ $invokeParams.Filter = @ ($thisTable.DescriptionField , ' -like' , $Description )
288
+ }
277
289
}
278
290
279
- if ( $invokeParams.Filter ) {
280
- $invokeParams.Filter = $invokeParams.Filter , ' and' , @ ($thisTable.DescriptionField , ' -like' , $Description )
281
- }
282
- else {
283
- $invokeParams.Filter = @ ($thisTable.DescriptionField , ' -like' , $Description )
291
+ $addedSysIdProp = $false
292
+ # we need the sys_id value in order to get custom var data
293
+ # add it in if specific properties were requested and not part of the list
294
+ if ( $IncludeCustomVariable.IsPresent ) {
295
+ if ( $Property -and ' sys_id' -notin $Property ) {
296
+ $invokeParams.Property += ' sys_id'
297
+ $addedSysIdProp = $true
298
+ }
284
299
}
285
- }
286
300
287
- $addedSysIdProp = $false
288
- # we need the sys_id value in order to get custom var data
289
- # add it in if specific properties were requested and not part of the list
290
- if ( $IncludeCustomVariable.IsPresent ) {
291
- if ( $Property -and ' sys_id' -notin $Property ) {
292
- $invokeParams.Property += ' sys_id'
293
- $addedSysIdProp = $true
301
+ # should use Get-ServiceNowAttachment, but put this here for ease of access
302
+ if ( $thisTable.Name -eq ' attachment' ) {
303
+ Write-Warning ' For attachments, use Get-ServiceNowAttachment'
304
+ $invokeParams.Remove (' Table' ) | Out-Null
305
+ $invokeParams.UriLeaf = ' /attachment'
294
306
}
295
- }
296
307
297
- # should use Get-ServiceNowAttachment, but put this here for ease of access
298
- if ( $thisTable.Name -eq ' attachment' ) {
299
- Write-Warning ' For attachments, use Get-ServiceNowAttachment'
300
- $invokeParams.Remove (' Table' ) | Out-Null
301
- $invokeParams.UriLeaf = ' /attachment'
302
- }
308
+ $result = Invoke-ServiceNowRestMethod @invokeParams
303
309
304
- $result = Invoke-ServiceNowRestMethod @invokeParams
310
+ if ( $IncludeCustomVariable ) {
311
+
312
+ # suppress warning when getting total count
313
+ $existingWarning = $WarningPreference
314
+ $WarningPreference = ' SilentlyContinue'
305
315
306
- if ( $result ) {
307
- if ( $IncludeCustomVariable.IsPresent ) {
308
316
# for each record, get the variable names and then get the variable values
309
317
foreach ($record in $result ) {
318
+
310
319
$customVarParams = @ {
311
- Table = ' sc_item_option_mtom'
312
- Property = ' sc_item_option.item_option_new.name ' , ' sc_item_option.item_option_new.sys_name ' , ' sc_item_option.item_option_new.type'
313
- Filter = @ ( ' request_item ' , ' -eq ' , $record .sys_id ) , ' and ' , @ ( ' sc_item_option.item_option_new.type' , ' -in ' , ' 1,2,3,4,5,6,7,8,9,10,16,18,21,22,26 ' )
314
- First = 1000 # hopefully there isn't more custom vars than this, but we need to overwrite the default of 10
320
+ Table = ' sc_item_option_mtom'
321
+ Filter = @ ( ' request_item ' , ' -eq ' , $record .sys_id ) , ' and ' , @ ( ' sc_item_option.item_option_new.type' , ' -in ' , ' 1,2,3,4,5,6,7,8,9,10,16,18,21,22,26 ' )
322
+ Property = ' sc_item_option.item_option_new.name ' , ' sc_item_option.value ' , ' sc_item_option.item_option_new.type' , ' sc_item_option.item_option_new.question_text '
323
+ IncludeTotalCount = $true
315
324
}
316
- $customVars = Get-ServiceNowRecord @customVarParams
317
325
318
- if ( $customVars ) {
319
- $customValueParams = @ {
320
- Table = $thisTable .Name
321
- Filter = @ ( ' sys_id ' , ' -eq ' , $record .sys_id )
322
- Property = $customVars . ' sc_item_option.item_option_new.name ' | ForEach-Object { " variables. $_ " }
323
- }
324
- $customValues = Get-ServiceNowRecord @customValueParams
325
-
326
- # custom vars will be a separate property on the return object
327
- $customVarsOut = $customVars | ForEach-Object {
328
- $varName = $_ .' sc_item_option.item_option_new.name '
329
- [ pscustomobject ] @ {
330
- Name = ' variables.{0} ' -f $varName
331
- DisplayName = $_ . ' sc_item_option.item_option_new.sys_name '
332
- Value = $customValues . " variables. $varName "
333
- }
334
- }
335
- $record | Add-Member @ {
336
- ' CustomVariable ' = $customVarsOut
326
+ $customVarsOut = Get-ServiceNowRecord @customVarParams
327
+
328
+ $record | Add-Member @ {
329
+ ' CustomVariable ' = $customVarsOut | Select-Object - Property `
330
+ @ {
331
+ ' n ' = ' Name '
332
+ ' e ' = { $_ . ' sc_item_option.item_option_new.name ' }
333
+ } ,
334
+ @ {
335
+ ' n ' = ' Value '
336
+ ' e ' = { $_ .' sc_item_option.value ' }
337
+ } ,
338
+ @ {
339
+ ' n ' = ' DisplayName '
340
+ ' e ' = { $_ . ' sc_item_option.item_option_new.question_text ' }
341
+ } ,
342
+ @ {
343
+ ' n ' = ' Type '
344
+ ' e ' = { $_ . ' sc_item_option.item_option_new.type ' }
337
345
}
338
346
}
339
347
@@ -344,13 +352,17 @@ function Get-ServiceNowRecord {
344
352
$record
345
353
}
346
354
}
355
+
356
+ $WarningPreference = $existingWarning
357
+
347
358
}
348
359
else {
349
360
350
361
# format the results
351
362
if ( $Property ) {
352
363
if ( $Property.Count -eq 1 -and $AsValue ) {
353
- $result | Select-Object - ExpandProperty $result.PSObject.Properties.Name
364
+ $propName = $result | Get-Member | Where-Object { $_.MemberType -eq ' NoteProperty' } | Select-Object - exp Name
365
+ $result | Select-Object - ExpandProperty $propName
354
366
}
355
367
else {
356
368
$result
0 commit comments