11
11
Name of the table to be queried, by either table name or class name. Use tab completion for list of known tables.
12
12
You can also provide any table name ad hoc.
13
13
14
- . PARAMETER Properties
14
+ . PARAMETER Property
15
15
Limit the fields returned to this list
16
16
17
17
. PARAMETER Filter
26
26
Array or multidimensional array of fields to sort on.
27
27
Each array should be of the format @(field, asc/desc).
28
28
29
- . PARAMETER DisplayValues
29
+ . PARAMETER DisplayValue
30
30
Option to display values for reference fields.
31
31
'false' will only retrieve the reference
32
32
'true' will only retrieve the underlying value
33
33
'all' will retrieve both. This is helpful when trying to translate values for a query.
34
34
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
+
35
41
. PARAMETER Connection
36
42
Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
37
43
55
61
. EXAMPLE
56
62
Get-ServiceNowRecord -Table incident -Filter @('state', '-eq', '1') -Sort @('opened_at', 'desc'), @('state')
57
63
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
+ ]
59
65
. EXAMPLE
60
66
Get-ServiceNowRecord -Table 'change request' -Filter @('opened_at', '-ge', 'javascript:gs.daysAgoEnd(30)')
61
67
Get change requests opened in the last 30 days. Use class name as opposed to table name.
64
70
Get-ServiceNowRecord -Table 'change request' -First 100 -IncludeTotalCount
65
71
Get all change requests, paging 100 at a time.
66
72
73
+ . EXAMPLE
74
+ Get-ServiceNowRecord -Table 'change request' -IncludeCustomVariable -First 5
75
+ Get the first 5 change requests and retrieve custom variable info
76
+
67
77
. INPUTS
68
78
None
69
79
@@ -84,8 +94,8 @@ function Get-ServiceNowRecord {
84
94
[string ] $Table ,
85
95
86
96
[Parameter ()]
87
- [Alias (' Fields' )]
88
- [string []] $Properties ,
97
+ [Alias (' Fields' , ' Properties ' )]
98
+ [string []] $Property ,
89
99
90
100
[parameter (ParameterSetName = ' AutomationFilter' )]
91
101
[parameter (ParameterSetName = ' SessionFilter' )]
@@ -98,7 +108,11 @@ function Get-ServiceNowRecord {
98
108
99
109
[Parameter ()]
100
110
[ValidateSet (' true' , ' false' , ' all' )]
101
- [string ] $DisplayValues = ' true' ,
111
+ [Alias (' DisplayValues' )]
112
+ [string ] $DisplayValue = ' true' ,
113
+
114
+ [Parameter ()]
115
+ [switch ] $IncludeCustomVariable ,
102
116
103
117
[Parameter (Mandatory , ParameterSetName = ' AutomationQuery' )]
104
118
[parameter (Mandatory , ParameterSetName = ' AutomationFilter' )]
@@ -111,14 +125,74 @@ function Get-ServiceNowRecord {
111
125
[hashtable ] $ServiceNowSession = $script :ServiceNowSession
112
126
)
113
127
114
- $result = Invoke-ServiceNowRestMethod @PSBoundParameters
128
+ $invokeParams = $PSBoundParameters
129
+ $invokeParams.Remove (' IncludeCustomVariable' ) | Out-Null
115
130
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
120
195
}
121
196
}
122
197
123
- $result
124
198
}
0 commit comments