Skip to content

Commit 3ab8104

Browse files
committed
rename order to sort, help updates
1 parent 33bd644 commit 3ab8104

File tree

3 files changed

+64
-25
lines changed

3 files changed

+64
-25
lines changed

ServiceNow/Private/Invoke-ServiceNowRestMethod.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function Invoke-ServiceNowRestMethod {
4444

4545
[parameter()]
4646
[ValidateNotNullOrEmpty()]
47-
[System.Collections.ArrayList] $Order = @('opened_at', 'desc'),
47+
[System.Collections.ArrayList] $Sort = @('opened_at', 'desc'),
4848

4949
# sysparm_query param in the format of a ServiceNow encoded query string (see http://wiki.servicenow.com/index.php?title=Encoded_Query_Strings)
5050
[Parameter()]
@@ -143,7 +143,7 @@ function Invoke-ServiceNowRestMethod {
143143
if ($Query) {
144144
$Body.sysparm_query = $Query
145145
} else {
146-
$body.sysparm_query = (New-ServiceNowQuery -Filter $Filter -Order $Order)
146+
$body.sysparm_query = (New-ServiceNowQuery -Filter $Filter -Sort $Sort)
147147
}
148148

149149
if ($Properties) {

ServiceNow/Public/Get-ServiceNowRecord.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function Get-ServiceNowRecord {
2929
[parameter(ParameterSetName = 'AutomationFilter')]
3030
[parameter(ParameterSetName = 'SessionFilter')]
3131
[ValidateNotNullOrEmpty()]
32-
[System.Collections.ArrayList] $Order,
32+
[System.Collections.ArrayList] $Sort,
3333

3434
# sysparm_query param in the format of a ServiceNow encoded query string (see http://wiki.servicenow.com/index.php?title=Encoded_Query_Strings)
3535
[Parameter(Mandatory, ParameterSetName = 'AutomationQuery')]

ServiceNow/Public/New-ServiceNowQuery.ps1

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,61 @@
1+
<#
2+
.SYNOPSIS
3+
Build query string for api call
4+
5+
.DESCRIPTION
6+
Build query string for api call, there are basic and advanced methods; see the different parameter sets.
7+
8+
Basic allows you to look for exact matches as well as fields that are like a value; these are all and'd together.
9+
You can also sort your results, ascending or descending, by 1 field.
10+
11+
Advanced allows you to perform the complete set of operations that ServiceNow has (mostly).
12+
The comparison operators have been made to mimic powershell itself so the code should be easy to understand.
13+
You can use a very large set of comparison operators (see the script variable ServiceNowOperator),
14+
and, or, and grouping joins, as well as multiple sorting parameters.
15+
16+
.PARAMETER Filter
17+
Array or multidimensional array of fields and values to filter on.
18+
Each array should be of the format @(field, comparison operator, value) separated by a join, either 'and', 'or', or 'group.
19+
For a complete list of comparison operators, see $script:ServiceNowOperator.
20+
See the examples.
21+
22+
.PARAMETER Sort
23+
Array or multidimensional array of fields to sort on.
24+
Each array should be of the format @(field, asc/desc).
25+
26+
.EXAMPLE
27+
New-ServiceNowQuery -MatchExact @{field_name=value}
28+
Get query string where field name exactly matches the value
29+
30+
.EXAMPLE
31+
New-ServiceNowQuery -MatchContains @{field_name=value}
32+
Get query string where field name contains the value
33+
34+
.EXAMPLE
35+
New-ServiceNowQuery -Filter @('state', '-eq', '1'), 'or', @('short_description','-like', 'powershell')
36+
Get query string where state equals New or short description contains the word powershell
37+
38+
.EXAMPLE
39+
$filter = @('state', '-eq', '1'),
40+
'and',
41+
@('short_description','-like', 'powershell'),
42+
'group',
43+
@('state', '-eq', '2')
44+
PS > New-ServiceNowQuery -Filter $filter
45+
Get query string where state equals New and short description contains the word powershell or state equals In Progress.
46+
The first 2 filters are combined and then or'd against the last.
47+
48+
.EXAMPLE
49+
New-ServiceNowQuery -Filter @('state', '-eq', '1') -Sort @('opened_at', 'desc'), @('state')
50+
Get query string where state equals New and first sort by the field opened_at descending and then sort by the field state ascending
51+
52+
.INPUTS
53+
None
54+
55+
.OUTPUTS
56+
String
57+
#>
158
function New-ServiceNowQuery {
2-
<#
3-
.SYNOPSIS
4-
Build query string for api call
5-
.DESCRIPTION
6-
Build query string for api call
7-
.EXAMPLE
8-
New-ServiceNowQuery -MatchExact @{field_name=value}
9-
10-
Get query string where field name exactly matches the value
11-
.EXAMPLE
12-
New-ServiceNowQuery -MatchContains @{field_name=value}
13-
14-
Get query string where field name contains the value
15-
.INPUTS
16-
None
17-
.OUTPUTS
18-
String
19-
#>
2059

2160
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseShouldProcessForStateChangingFunctions', 'No state is actually changing')]
2261

@@ -46,7 +85,7 @@ function New-ServiceNowQuery {
4685

4786
[parameter(ParameterSetName = 'Advanced')]
4887
[ValidateNotNullOrEmpty()]
49-
[System.Collections.ArrayList] $Order = @('opened_at', 'desc')
88+
[System.Collections.ArrayList] $Sort
5089

5190
)
5291

@@ -139,11 +178,11 @@ function New-ServiceNowQuery {
139178
$query += '^'
140179
}
141180

142-
$orderList = $Order
181+
$orderList = $Sort
143182
# see if we're working with 1 array or multidimensional array
144183
# we're looking for multidimensional so convert if not
145-
if ($Order[0].GetType().Name -eq 'String') {
146-
$orderList = @(, $Order)
184+
if ($Sort[0].GetType().Name -eq 'String') {
185+
$orderList = @(, $Sort)
147186
}
148187

149188
$query += for ($i = 0; $i -lt $orderList.Count; $i++) {

0 commit comments

Comments
 (0)