Skip to content

Commit 0a639f8

Browse files
authored
Merge pull request #140 from Snow-Shell/get-custom-variables
Get custom variables
2 parents f4f0df7 + e65d585 commit 0a639f8

File tree

5 files changed

+97
-21
lines changed

5 files changed

+97
-21
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 2.4.1
2+
- Add `-IncludeCustomVariable` to `Get-ServiceNowRecord` to retrieve custom variables, eg. ritm form values, in addition to the standard fields. [#138](https://github.com/Snow-Shell/servicenow-powershell/discussions/138)
3+
14
## 2.4
25
- Add `New-ServiceNowConfigurationItem`, [#109](https://github.com/Snow-Shell/servicenow-powershell/issues/109)
36
- Add grouping operators -and and -group as well as comparison operators -startwith and -endswith to `Get-ServiceNowRecord -Filter` to keep with the -operator standard

ServiceNow/Private/Invoke-ServiceNowRestMethod.ps1

+7-7
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ function Invoke-ServiceNowRestMethod {
4444
[System.Collections.ArrayList] $Filter,
4545

4646
[parameter()]
47-
[ValidateNotNullOrEmpty()]
4847
[System.Collections.ArrayList] $Sort = @('opened_at', 'desc'),
4948

5049
# sysparm_query param in the format of a ServiceNow encoded query string (see http://wiki.servicenow.com/index.php?title=Encoded_Query_Strings)
@@ -53,13 +52,14 @@ function Invoke-ServiceNowRestMethod {
5352

5453
# Fields to return
5554
[Parameter()]
56-
[Alias('Fields')]
57-
[string[]] $Properties,
55+
[Alias('Fields', 'Properties')]
56+
[string[]] $Property,
5857

5958
# Whether or not to show human readable display values instead of machine values
6059
[Parameter()]
6160
[ValidateSet('true', 'false', 'all')]
62-
[string] $DisplayValues = 'true',
61+
[Alias('DisplayValues')]
62+
[string] $DisplayValue = 'true',
6363

6464
[Parameter()]
6565
[PSCredential] $Credential,
@@ -105,7 +105,7 @@ function Invoke-ServiceNowRestMethod {
105105

106106
if ( $Method -eq 'Get') {
107107
$Body = @{
108-
'sysparm_display_value' = $DisplayValues
108+
'sysparm_display_value' = $DisplayValue
109109
'sysparm_query' = (New-ServiceNowQuery -Filter $Filter -Sort $Sort)
110110
'sysparm_limit' = 10
111111
}
@@ -129,8 +129,8 @@ function Invoke-ServiceNowRestMethod {
129129
$Body.sysparm_query = $Query
130130
}
131131

132-
if ($Properties) {
133-
$Body.sysparm_fields = ($Properties -join ',').ToLower()
132+
if ($Property) {
133+
$Body.sysparm_fields = ($Property -join ',').ToLower()
134134
}
135135
}
136136

ServiceNow/Public/Get-ServiceNowRecord.ps1

+86-12
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
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
@@ -26,12 +26,18 @@
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
@@ -55,7 +61,7 @@
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.
@@ -64,6 +70,10 @@
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
}

ServiceNow/Public/New-ServiceNowQuery.ps1

-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ function New-ServiceNowQuery {
9090
[System.Collections.ArrayList] $Filter,
9191

9292
[parameter(ParameterSetName = 'Advanced')]
93-
[ValidateNotNullOrEmpty()]
9493
[System.Collections.ArrayList] $Sort
9594

9695
)

ServiceNow/ServiceNow.psd1

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
RootModule = 'ServiceNow.psm1'
66

77
# Version number of this module.
8-
ModuleVersion = '2.4'
8+
ModuleVersion = '2.4.1'
99

1010
# ID used to uniquely identify this module
1111
GUID = 'b90d67da-f8d0-4406-ad74-89d169cd0633'

0 commit comments

Comments
 (0)