48
48
Some records may have associated custom variables, some may not.
49
49
For instance, an RITM may have custom variables, but the associated tasks may not.
50
50
A property named 'CustomVariable' will be added to the return object.
51
+ When used with -New, you can now get the value with $return.CustomVariable.CustomVarName.Value.
52
+
53
+ . PARAMETER New
54
+ Used with -IncludeCustomVariable for the new format.
55
+ Each property is now added by name with a value of .value.
51
56
52
57
. PARAMETER AsValue
53
58
Return the underlying value instead of pscustomobject.
107
112
Get all change requests, paging 100 at a time.
108
113
109
114
. EXAMPLE
110
- Get-ServiceNowRecord -Table 'change request' -IncludeCustomVariable -First 5
115
+ Get-ServiceNowRecord -Table 'change request' -IncludeCustomVariable -New - First 5
111
116
Get the first 5 change requests and retrieve custom variable info
112
117
113
118
. EXAMPLE
@@ -146,8 +151,7 @@ function Get-ServiceNowRecord {
146
151
[ValidateScript ( {
147
152
if ($_ -match ' ^[a-zA-Z0-9]{32}$' -or $_ -match ' ^([a-zA-Z]+)[0-9]+$' ) {
148
153
$true
149
- }
150
- else {
154
+ } else {
151
155
throw ' Id must be either a 32 character alphanumeric, ServiceNow sysid, or prefix/id, ServiceNow number.'
152
156
}
153
157
})]
@@ -158,8 +162,7 @@ function Get-ServiceNowRecord {
158
162
[ValidateScript ( {
159
163
if ($_ -match ' ^[a-zA-Z0-9]{32}$' -or $_ -match ' ^([a-zA-Z]+)[0-9]+$' ) {
160
164
$true
161
- }
162
- else {
165
+ } else {
163
166
throw ' ParentId must be either a 32 character alphanumeric, ServiceNow sysid, or prefix/id, ServiceNow number.'
164
167
}
165
168
})]
@@ -187,6 +190,9 @@ function Get-ServiceNowRecord {
187
190
[Parameter ()]
188
191
[switch ] $IncludeCustomVariable ,
189
192
193
+ [Parameter ()]
194
+ [switch ] $New ,
195
+
190
196
[Parameter ()]
191
197
[switch ] $AsValue ,
192
198
@@ -235,8 +241,7 @@ function Get-ServiceNowRecord {
235
241
}
236
242
237
243
$idFilter = @ (' sys_id' , ' -eq' , $ID )
238
- }
239
- else {
244
+ } else {
240
245
if ( -not $thisTable ) {
241
246
# get table name from prefix if only Id was provided
242
247
$idPrefix = ($ID | Select-String - Pattern ' ^([a-zA-Z]+)([0-9]+$)' ).Matches.Groups[1 ].Value.ToLower()
@@ -251,8 +256,7 @@ function Get-ServiceNowRecord {
251
256
252
257
if ( $invokeParams.Filter ) {
253
258
$invokeParams.Filter = $invokeParams.Filter , ' and' , $idFilter
254
- }
255
- else {
259
+ } else {
256
260
$invokeParams.Filter = $idFilter
257
261
}
258
262
@@ -264,15 +268,13 @@ function Get-ServiceNowRecord {
264
268
if ( $ParentID ) {
265
269
if ( $ParentID -match ' ^[a-zA-Z0-9]{32}$' ) {
266
270
$parentIdFilter = @ (' parent.sys_id' , ' -eq' , $ParentID )
267
- }
268
- else {
271
+ } else {
269
272
$parentIdFilter = @ (' parent.number' , ' -eq' , $ParentID )
270
273
}
271
274
272
275
if ( $invokeParams.Filter ) {
273
276
$invokeParams.Filter = $invokeParams.Filter , ' and' , $parentIdFilter
274
- }
275
- else {
277
+ } else {
276
278
$invokeParams.Filter = $parentIdFilter
277
279
}
278
280
}
@@ -286,8 +288,7 @@ function Get-ServiceNowRecord {
286
288
287
289
if ( $invokeParams.Filter ) {
288
290
$invokeParams.Filter = $invokeParams.Filter , ' and' , @ ($thisTable.DescriptionField , ' -like' , $Description )
289
- }
290
- else {
291
+ } else {
291
292
$invokeParams.Filter = @ ($thisTable.DescriptionField , ' -like' , $Description )
292
293
}
293
294
}
@@ -313,10 +314,6 @@ function Get-ServiceNowRecord {
313
314
314
315
if ( $IncludeCustomVariable ) {
315
316
316
- # suppress warning when getting total count
317
- $existingWarning = $WarningPreference
318
- $WarningPreference = ' SilentlyContinue'
319
-
320
317
# for each record, get the variable names and then get the variable values
321
318
foreach ($record in $result ) {
322
319
@@ -325,54 +322,70 @@ function Get-ServiceNowRecord {
325
322
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' )
326
323
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'
327
324
IncludeTotalCount = $true
325
+ ServiceNowSession = $ServiceNowSession
328
326
}
329
327
330
- $customVarsOut = Get-ServiceNowRecord @customVarParams
331
-
332
- $record | Add-Member @ {
333
- ' CustomVariable' = $customVarsOut | Select-Object - Property `
334
- @ {
335
- ' n' = ' Name'
336
- ' e' = { $_ .' sc_item_option.item_option_new.name' }
337
- },
338
- @ {
339
- ' n' = ' Value'
340
- ' e' = { $_ .' sc_item_option.value' }
341
- },
342
- @ {
343
- ' n' = ' DisplayName'
344
- ' e' = { $_ .' sc_item_option.item_option_new.question_text' }
345
- },
346
- @ {
347
- ' n' = ' Type'
348
- ' e' = { $_ .' sc_item_option.item_option_new.type' }
328
+ # suppress warning when getting total count
329
+ $customVarsOut = Get-ServiceNowRecord @customVarParams - WarningAction SilentlyContinue
330
+
331
+ if ( $New ) {
332
+
333
+ $record | Add-Member @ {
334
+ ' CustomVariable' = [pscustomobject ]@ {}
335
+ }
336
+ foreach ($var in $customVarsOut ) {
337
+ $record.CustomVariable | Add-Member @ {
338
+ $var .' sc_item_option.item_option_new.name' = @ {
339
+ Value = $var .' sc_item_option.value'
340
+ DisplayName = $var .' sc_item_option.item_option_new.question_text'
341
+ Type = $var .' sc_item_option.item_option_new.type'
342
+ }
343
+ }
344
+ }
345
+ } else {
346
+
347
+ Write-Warning ' The format for custom variables will soon change. Start using -New with -IncludeCustomVariable to preview.'
348
+
349
+ $record | Add-Member @ {
350
+ ' CustomVariable' = $customVarsOut | Select-Object - Property `
351
+ @ {
352
+ ' n' = ' Name'
353
+ ' e' = { $_ .' sc_item_option.item_option_new.name' }
354
+ },
355
+ @ {
356
+ ' n' = ' Value'
357
+ ' e' = { $_ .' sc_item_option.value' }
358
+ },
359
+ @ {
360
+ ' n' = ' DisplayName'
361
+ ' e' = { $_ .' sc_item_option.item_option_new.question_text' }
362
+ },
363
+ @ {
364
+ ' n' = ' Type'
365
+ ' e' = { $_ .' sc_item_option.item_option_new.type' }
366
+ }
349
367
}
350
368
}
351
369
370
+
352
371
if ( $addedSysIdProp ) {
353
372
$record | Select-Object - Property * - ExcludeProperty sys_id
354
- }
355
- else {
373
+ } else {
356
374
$record
357
375
}
358
376
}
359
377
360
- $WarningPreference = $existingWarning
361
-
362
- }
363
- else {
378
+ } else {
364
379
365
380
# format the results
366
381
if ( $Property ) {
367
382
if ( $Property.Count -eq 1 -and $AsValue ) {
368
383
$propName = $result | Get-Member | Where-Object { $_.MemberType -eq ' NoteProperty' } | Select-Object - exp Name
369
384
$result | Select-Object - ExpandProperty $propName
370
- }
371
- else {
385
+ } else {
372
386
$result
373
387
}
374
- }
375
- else {
388
+ } else {
376
389
if ($thisTable.Type ) {
377
390
$result | ForEach-Object { $_.PSObject.TypeNames.Insert (0 , $thisTable.Type ) }
378
391
}
0 commit comments