3434
3535. PARAMETER IncludeCustomVariable
3636 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.
3739
3840. PARAMETER Connection
3941 Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
5860. EXAMPLE
5961 Get-ServiceNowRecord -Table incident -Filter @('state', '-eq', '1') -Sort @('opened_at', 'desc'), @('state')
6062 Get incident records where state equals New and first sort by the field opened_at descending and then sort by the field state ascending
61-
63+ ]
6264. EXAMPLE
6365 Get-ServiceNowRecord -Table 'change request' -Filter @('opened_at', '-ge', 'javascript:gs.daysAgoEnd(30)')
6466 Get change requests opened in the last 30 days. Use class name as opposed to table name.
@@ -122,16 +124,6 @@ function Get-ServiceNowRecord {
122124 [hashtable ] $ServiceNowSession = $script :ServiceNowSession
123125 )
124126
125- # $invokeParams = @{
126- # Table = $Table
127- # Properties = $Property
128- # Filter = $Filter
129- # Sort = $Sort
130- # DisplayValues = $DisplayValue
131- # Connection = $Connection
132- # ServiceNowSession = $ServiceNowSession
133- # }
134-
135127 $invokeParams = $PSBoundParameters
136128 $invokeParams.Remove (' IncludeCustomVariable' ) | Out-Null
137129
@@ -156,7 +148,7 @@ function Get-ServiceNowRecord {
156148 Table = ' sc_item_option_mtom'
157149 Property = ' sc_item_option.item_option_new.name' , ' sc_item_option.item_option_new.sys_name' , ' sc_item_option.item_option_new.type'
158150 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' )
159- First = 1000 # hopefully there isn't more custom vars than this...
151+ First = 1000 # hopefully there isn't more custom vars than this, but we need to overwrite the default of 10
160152 }
161153 $customVars = Get-ServiceNowRecord @customVarParams
162154
@@ -167,11 +159,19 @@ function Get-ServiceNowRecord {
167159 Property = $customVars .' sc_item_option.item_option_new.name' | ForEach-Object { " variables.$_ " }
168160 }
169161 $customValues = Get-ServiceNowRecord @customValueParams
170- $customValues | Get-Member - MemberType NoteProperty | ForEach-Object {
171- $record | Add-Member @ {
172- $_.Name = $customValues ." $ ( $_.Name ) "
162+
163+ # custom vars will be a separate property on the return object
164+ $customVarsOut = $customVars | ForEach-Object {
165+ $varName = $_ .' sc_item_option.item_option_new.name'
166+ [pscustomobject ] @ {
167+ Name = ' variables.{0}' -f $varName
168+ DisplayName = $_ .' sc_item_option.item_option_new.sys_name'
169+ Value = $customValues ." variables.$varName "
173170 }
174171 }
172+ $record | Add-Member @ {
173+ ' CustomVariable' = $customVarsOut
174+ }
175175 }
176176
177177 if ( $addedSysIdProp ) {
0 commit comments