4848 Some records may have associated custom variables, some may not.
4949 For instance, an RITM may have custom variables, but the associated tasks may not.
5050 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.
5156
5257. PARAMETER AsValue
5358 Return the underlying value instead of pscustomobject.
107112 Get all change requests, paging 100 at a time.
108113
109114. EXAMPLE
110- Get-ServiceNowRecord -Table 'change request' -IncludeCustomVariable -First 5
115+ Get-ServiceNowRecord -Table 'change request' -IncludeCustomVariable -New - First 5
111116 Get the first 5 change requests and retrieve custom variable info
112117
113118. EXAMPLE
@@ -146,8 +151,7 @@ function Get-ServiceNowRecord {
146151 [ValidateScript ( {
147152 if ($_ -match ' ^[a-zA-Z0-9]{32}$' -or $_ -match ' ^([a-zA-Z]+)[0-9]+$' ) {
148153 $true
149- }
150- else {
154+ } else {
151155 throw ' Id must be either a 32 character alphanumeric, ServiceNow sysid, or prefix/id, ServiceNow number.'
152156 }
153157 })]
@@ -158,8 +162,7 @@ function Get-ServiceNowRecord {
158162 [ValidateScript ( {
159163 if ($_ -match ' ^[a-zA-Z0-9]{32}$' -or $_ -match ' ^([a-zA-Z]+)[0-9]+$' ) {
160164 $true
161- }
162- else {
165+ } else {
163166 throw ' ParentId must be either a 32 character alphanumeric, ServiceNow sysid, or prefix/id, ServiceNow number.'
164167 }
165168 })]
@@ -187,6 +190,9 @@ function Get-ServiceNowRecord {
187190 [Parameter ()]
188191 [switch ] $IncludeCustomVariable ,
189192
193+ [Parameter ()]
194+ [switch ] $New ,
195+
190196 [Parameter ()]
191197 [switch ] $AsValue ,
192198
@@ -235,8 +241,7 @@ function Get-ServiceNowRecord {
235241 }
236242
237243 $idFilter = @ (' sys_id' , ' -eq' , $ID )
238- }
239- else {
244+ } else {
240245 if ( -not $thisTable ) {
241246 # get table name from prefix if only Id was provided
242247 $idPrefix = ($ID | Select-String - Pattern ' ^([a-zA-Z]+)([0-9]+$)' ).Matches.Groups[1 ].Value.ToLower()
@@ -251,8 +256,7 @@ function Get-ServiceNowRecord {
251256
252257 if ( $invokeParams.Filter ) {
253258 $invokeParams.Filter = $invokeParams.Filter , ' and' , $idFilter
254- }
255- else {
259+ } else {
256260 $invokeParams.Filter = $idFilter
257261 }
258262
@@ -264,15 +268,13 @@ function Get-ServiceNowRecord {
264268 if ( $ParentID ) {
265269 if ( $ParentID -match ' ^[a-zA-Z0-9]{32}$' ) {
266270 $parentIdFilter = @ (' parent.sys_id' , ' -eq' , $ParentID )
267- }
268- else {
271+ } else {
269272 $parentIdFilter = @ (' parent.number' , ' -eq' , $ParentID )
270273 }
271274
272275 if ( $invokeParams.Filter ) {
273276 $invokeParams.Filter = $invokeParams.Filter , ' and' , $parentIdFilter
274- }
275- else {
277+ } else {
276278 $invokeParams.Filter = $parentIdFilter
277279 }
278280 }
@@ -286,8 +288,7 @@ function Get-ServiceNowRecord {
286288
287289 if ( $invokeParams.Filter ) {
288290 $invokeParams.Filter = $invokeParams.Filter , ' and' , @ ($thisTable.DescriptionField , ' -like' , $Description )
289- }
290- else {
291+ } else {
291292 $invokeParams.Filter = @ ($thisTable.DescriptionField , ' -like' , $Description )
292293 }
293294 }
@@ -313,10 +314,6 @@ function Get-ServiceNowRecord {
313314
314315 if ( $IncludeCustomVariable ) {
315316
316- # suppress warning when getting total count
317- $existingWarning = $WarningPreference
318- $WarningPreference = ' SilentlyContinue'
319-
320317 # for each record, get the variable names and then get the variable values
321318 foreach ($record in $result ) {
322319
@@ -325,54 +322,70 @@ function Get-ServiceNowRecord {
325322 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' )
326323 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'
327324 IncludeTotalCount = $true
325+ ServiceNowSession = $ServiceNowSession
328326 }
329327
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+ }
349367 }
350368 }
351369
370+
352371 if ( $addedSysIdProp ) {
353372 $record | Select-Object - Property * - ExcludeProperty sys_id
354- }
355- else {
373+ } else {
356374 $record
357375 }
358376 }
359377
360- $WarningPreference = $existingWarning
361-
362- }
363- else {
378+ } else {
364379
365380 # format the results
366381 if ( $Property ) {
367382 if ( $Property.Count -eq 1 -and $AsValue ) {
368383 $propName = $result | Get-Member | Where-Object { $_.MemberType -eq ' NoteProperty' } | Select-Object - exp Name
369384 $result | Select-Object - ExpandProperty $propName
370- }
371- else {
385+ } else {
372386 $result
373387 }
374- }
375- else {
388+ } else {
376389 if ($thisTable.Type ) {
377390 $result | ForEach-Object { $_.PSObject.TypeNames.Insert (0 , $thisTable.Type ) }
378391 }
0 commit comments