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+ Some records may have associated custom variables, some may not.
38+ For instance, an RITM may have custom variables, but the associated tasks may not.
39+ A property named 'CustomVariable' will be added to the return object.
40+
3541. PARAMETER Connection
3642 Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
3743
5561. EXAMPLE
5662 Get-ServiceNowRecord -Table incident -Filter @('state', '-eq', '1') -Sort @('opened_at', 'desc'), @('state')
5763 Get incident records where state equals New and first sort by the field opened_at descending and then sort by the field state ascending
58-
64+ ]
5965. EXAMPLE
6066 Get-ServiceNowRecord -Table 'change request' -Filter @('opened_at', '-ge', 'javascript:gs.daysAgoEnd(30)')
6167 Get change requests opened in the last 30 days. Use class name as opposed to table name.
6470 Get-ServiceNowRecord -Table 'change request' -First 100 -IncludeTotalCount
6571 Get all change requests, paging 100 at a time.
6672
73+ . EXAMPLE
74+ Get-ServiceNowRecord -Table 'change request' -IncludeCustomVariable -First 5
75+ Get the first 5 change requests and retrieve custom variable info
76+
6777. INPUTS
6878 None
6979
@@ -84,8 +94,8 @@ function Get-ServiceNowRecord {
8494 [string ] $Table ,
8595
8696 [Parameter ()]
87- [Alias (' Fields' )]
88- [string []] $Properties ,
97+ [Alias (' Fields' , ' Properties ' )]
98+ [string []] $Property ,
8999
90100 [parameter (ParameterSetName = ' AutomationFilter' )]
91101 [parameter (ParameterSetName = ' SessionFilter' )]
@@ -98,7 +108,11 @@ function Get-ServiceNowRecord {
98108
99109 [Parameter ()]
100110 [ValidateSet (' true' , ' false' , ' all' )]
101- [string ] $DisplayValues = ' true' ,
111+ [Alias (' DisplayValues' )]
112+ [string ] $DisplayValue = ' true' ,
113+
114+ [Parameter ()]
115+ [switch ] $IncludeCustomVariable ,
102116
103117 [Parameter (Mandatory , ParameterSetName = ' AutomationQuery' )]
104118 [parameter (Mandatory , ParameterSetName = ' AutomationFilter' )]
@@ -111,14 +125,74 @@ function Get-ServiceNowRecord {
111125 [hashtable ] $ServiceNowSession = $script :ServiceNowSession
112126 )
113127
114- $result = Invoke-ServiceNowRestMethod @PSBoundParameters
128+ $invokeParams = $PSBoundParameters
129+ $invokeParams.Remove (' IncludeCustomVariable' ) | Out-Null
115130
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 ) }
131+ $addedSysIdProp = $false
132+
133+ # we need the sys_id value in order to get custom var data
134+ # add it in if specific properties were requested and not part of the list
135+ if ( $IncludeCustomVariable.IsPresent ) {
136+ if ( $Property -and ' sys_id' -notin $Property ) {
137+ $invokeParams.Property += ' sys_id'
138+ $addedSysIdProp = $true
139+ }
140+ }
141+
142+ $result = Invoke-ServiceNowRestMethod @invokeParams
143+
144+ if ( $result ) {
145+ if ( $IncludeCustomVariable.IsPresent ) {
146+ # for each record, get the variable names and then get the variable values
147+ foreach ($record in $result ) {
148+ $customVarParams = @ {
149+ Table = ' sc_item_option_mtom'
150+ Property = ' sc_item_option.item_option_new.name' , ' sc_item_option.item_option_new.sys_name' , ' sc_item_option.item_option_new.type'
151+ 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' )
152+ First = 1000 # hopefully there isn't more custom vars than this, but we need to overwrite the default of 10
153+ }
154+ $customVars = Get-ServiceNowRecord @customVarParams
155+
156+ if ( $customVars ) {
157+ $customValueParams = @ {
158+ Table = $Table
159+ Filter = @ (' sys_id' , ' -eq' , $record.sys_id )
160+ Property = $customVars .' sc_item_option.item_option_new.name' | ForEach-Object { " variables.$_ " }
161+ }
162+ $customValues = Get-ServiceNowRecord @customValueParams
163+
164+ # custom vars will be a separate property on the return object
165+ $customVarsOut = $customVars | ForEach-Object {
166+ $varName = $_ .' sc_item_option.item_option_new.name'
167+ [pscustomobject ] @ {
168+ Name = ' variables.{0}' -f $varName
169+ DisplayName = $_ .' sc_item_option.item_option_new.sys_name'
170+ Value = $customValues ." variables.$varName "
171+ }
172+ }
173+ $record | Add-Member @ {
174+ ' CustomVariable' = $customVarsOut
175+ }
176+ }
177+
178+ if ( $addedSysIdProp ) {
179+ $record | Select-Object - Property * - ExcludeProperty sys_id
180+ }
181+ else {
182+ $record
183+ }
184+ }
185+ }
186+ else {
187+
188+ if ( -not $Property ) {
189+ $type = $script :ServiceNowTable | Where-Object { $_.Name -eq $Table -or $_.ClassName -eq $Table } | Select-Object - ExpandProperty Type
190+ if ($type ) {
191+ $result | ForEach-Object { $_.PSObject.TypeNames.Insert (0 , $type ) }
192+ }
193+ }
194+ $result
120195 }
121196 }
122197
123- $result
124198}
0 commit comments