@@ -138,8 +138,8 @@ function Get-ServiceNowRecord {
138138 [Alias (' sys_class_name' )]
139139 [string ] $Table ,
140140
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 )]
143143 [ValidateScript ( {
144144 if ($_ -match ' ^[a-zA-Z0-9]{32}$' -or $_ -match ' ^([a-zA-Z]+)[0-9]+$' ) {
145145 $true
@@ -194,146 +194,154 @@ function Get-ServiceNowRecord {
194194 [hashtable ] $ServiceNowSession = $script :ServiceNowSession
195195 )
196196
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+ }
208209
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+ }
219221 }
220222 }
221223 }
222224
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 {
228226
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}$' ) {
237229 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. '
239231 }
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
240253 }
241- $idFilter = @ (' number' , ' -eq' , $Id )
242- }
243254
244- if ( $invokeParams.Filter ) {
245- $invokeParams.Filter = $invokeParams.Filter , ' and' , $idFilter
246- }
247- else {
248- $invokeParams.Filter = $idFilter
249255 }
250256
251- }
257+ # we have the table, update the params
258+ $invokeParams.Table = $thisTable.Name
252259
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+ }
255267
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+ }
262274 }
263275
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+ }
271282
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+ }
277289 }
278290
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+ }
284299 }
285- }
286300
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'
294306 }
295- }
296307
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
303309
304- $result = Invoke-ServiceNowRestMethod @invokeParams
310+ if ( $IncludeCustomVariable ) {
311+
312+ # suppress warning when getting total count
313+ $existingWarning = $WarningPreference
314+ $WarningPreference = ' SilentlyContinue'
305315
306- if ( $result ) {
307- if ( $IncludeCustomVariable.IsPresent ) {
308316 # for each record, get the variable names and then get the variable values
309317 foreach ($record in $result ) {
318+
310319 $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
315324 }
316- $customVars = Get-ServiceNowRecord @customVarParams
317325
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 ' }
337345 }
338346 }
339347
@@ -344,13 +352,17 @@ function Get-ServiceNowRecord {
344352 $record
345353 }
346354 }
355+
356+ $WarningPreference = $existingWarning
357+
347358 }
348359 else {
349360
350361 # format the results
351362 if ( $Property ) {
352363 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
354366 }
355367 else {
356368 $result
0 commit comments