|
| 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 | +#> |
1 | 58 | 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 | | - #> |
20 | 59 |
|
21 | 60 | [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseShouldProcessForStateChangingFunctions', 'No state is actually changing')] |
22 | 61 |
|
@@ -46,7 +85,7 @@ function New-ServiceNowQuery { |
46 | 85 |
|
47 | 86 | [parameter(ParameterSetName = 'Advanced')] |
48 | 87 | [ValidateNotNullOrEmpty()] |
49 | | - [System.Collections.ArrayList] $Order = @('opened_at', 'desc') |
| 88 | + [System.Collections.ArrayList] $Sort |
50 | 89 |
|
51 | 90 | ) |
52 | 91 |
|
@@ -139,11 +178,11 @@ function New-ServiceNowQuery { |
139 | 178 | $query += '^' |
140 | 179 | } |
141 | 180 |
|
142 | | - $orderList = $Order |
| 181 | + $orderList = $Sort |
143 | 182 | # see if we're working with 1 array or multidimensional array |
144 | 183 | # 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) |
147 | 186 | } |
148 | 187 |
|
149 | 188 | $query += for ($i = 0; $i -lt $orderList.Count; $i++) { |
|
0 commit comments