1111 Name of the table to be queried, by either table name or class name. Use tab completion for list of known tables.
1212 You can also provide any table name ad hoc.
1313
14- . PARAMETER Properties
14+ . PARAMETER Property
1515 Limit the fields returned to this list
1616
1717. PARAMETER Filter
2626 Array or multidimensional array of fields to sort on.
2727 Each array should be of the format @(field, asc/desc).
2828
29- . PARAMETER DisplayValues
29+ . PARAMETER DisplayValue
3030 Option to display values for reference fields.
3131 'false' will only retrieve the reference
3232 'true' will only retrieve the underlying value
3333 'all' will retrieve both. This is helpful when trying to translate values for a query.
3434
35+ . PARAMETER IncludeCustomVariable
36+ Include custom variables in the return object.
37+
3538. PARAMETER Connection
3639 Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
3740
6467 Get-ServiceNowRecord -Table 'change request' -First 100 -IncludeTotalCount
6568 Get all change requests, paging 100 at a time.
6669
70+ . EXAMPLE
71+ Get-ServiceNowRecord -Table 'change request' -IncludeCustomVariable -First 5
72+ Get the first 5 change requests and retrieve custom variable info
73+
6774. INPUTS
6875 None
6976
@@ -84,8 +91,8 @@ function Get-ServiceNowRecord {
8491 [string ] $Table ,
8592
8693 [Parameter ()]
87- [Alias (' Fields' )]
88- [string []] $Properties ,
94+ [Alias (' Fields' , ' Properties ' )]
95+ [string []] $Property ,
8996
9097 [parameter (ParameterSetName = ' AutomationFilter' )]
9198 [parameter (ParameterSetName = ' SessionFilter' )]
@@ -98,7 +105,11 @@ function Get-ServiceNowRecord {
98105
99106 [Parameter ()]
100107 [ValidateSet (' true' , ' false' , ' all' )]
101- [string ] $DisplayValues = ' true' ,
108+ [Alias (' DisplayValues' )]
109+ [string ] $DisplayValue = ' true' ,
110+
111+ [Parameter ()]
112+ [switch ] $IncludeCustomVariable ,
102113
103114 [Parameter (Mandatory , ParameterSetName = ' AutomationQuery' )]
104115 [parameter (Mandatory , ParameterSetName = ' AutomationFilter' )]
@@ -111,14 +122,68 @@ function Get-ServiceNowRecord {
111122 [hashtable ] $ServiceNowSession = $script :ServiceNowSession
112123 )
113124
114- $result = Invoke-ServiceNowRestMethod @PSBoundParameters
125+ $invokeParams = @ {
126+ Table = $Table
127+ Properties = $Property
128+ Filter = $Filter
129+ Sort = $Sort
130+ DisplayValues = $DisplayValue
131+ Connection = $Connection
132+ ServiceNowSession = $ServiceNowSession
133+ }
115134
116- If ( $result -and -not $Properties ) {
117- $type = $script :ServiceNowTable | Where-Object {$_.Name -eq $Table -or $_.ClassName -eq $Table } | Select-Object - ExpandProperty Type
118- if ($type ) {
119- $result | ForEach-Object { $_.PSObject.TypeNames.Insert (0 , $type ) }
135+ $addedSysIdProp = $false
136+
137+ # we need the sys_id value in order to get custom var data
138+ # add it in if specific properties were requested and not part of the list
139+ if ( $IncludeCustomVariable.IsPresent ) {
140+ if ( $Property -and ' sys_id' -notin $Property ) {
141+ $invokeParams.Properties += ' sys_id'
142+ $addedSysIdProp = $true
143+ }
144+ }
145+
146+ $result = Invoke-ServiceNowRestMethod @invokeParams
147+
148+ if ( $result ) {
149+ if ( $IncludeCustomVariable.IsPresent ) {
150+ # for each record, get the variable names and then get the variable values
151+ foreach ($record in $result ) {
152+ $customVarParams = @ {
153+ Table = ' sc_item_option_mtom'
154+ Properties = ' sc_item_option.item_option_new.name' , ' sc_item_option.item_option_new.sys_name' , ' sc_item_option.item_option_new.type'
155+ 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' )
156+ First = 1000 # hopefully there isn't more custom vars than this...
157+ }
158+ $customVars = Get-ServiceNowRecord @customVarParams
159+
160+ if ( $customVars ) {
161+ $customValues = Get-ServiceNowRecord - Table $Table - Filter @ (' sys_id' , ' -eq' , $record.sys_id ) - Properties (' variables.' + ($customVars .' sc_item_option.item_option_new.name' -join ' ,variables.' ))
162+ $customValues | Get-Member - MemberType NoteProperty | ForEach-Object {
163+ $record | Add-Member @ {
164+ $_.Name = $customValues ." $ ( $_.Name ) "
165+ }
166+ }
167+ }
168+
169+ if ( $addedSysIdProp ) {
170+ $record | Select-Object - Property * - ExcludeProperty sys_id
171+ }
172+ else {
173+ $record
174+ }
175+ }
176+ }
177+ else {
178+
179+ if ( -not $Property ) {
180+ $type = $script :ServiceNowTable | Where-Object { $_.Name -eq $Table -or $_.ClassName -eq $Table } | Select-Object - ExpandProperty Type
181+ if ($type ) {
182+ $result | ForEach-Object { $_.PSObject.TypeNames.Insert (0 , $type ) }
183+ }
184+ }
185+ $result
120186 }
121187 }
122188
123- $result
124189}
0 commit comments