@@ -18,41 +18,189 @@ function New-ServiceNowQuery {
1818 String
1919 #>
2020
21- # This function doesn't change state. Doesn't justify ShouldProcess functionality
22- [System.Diagnostics.CodeAnalysis.SuppressMessage (' PSUseShouldProcessForStateChangingFunctions' , ' ' )]
21+ [System.Diagnostics.CodeAnalysis.SuppressMessage (' PSUseShouldProcessForStateChangingFunctions' , ' No state is actually changing' )]
2322
2423 [CmdletBinding ()]
2524 [OutputType ([System.String ])]
2625
2726 param (
2827 # Machine name of the field to order by
29- [parameter ()]
30- [string ]$OrderBy = ' opened_at' ,
28+ [parameter (ParameterSetName = ' Basic ' )]
29+ [string ] $OrderBy = ' opened_at' ,
3130
3231 # Direction of ordering (Desc/Asc)
33- [parameter ()]
32+ [parameter (ParameterSetName = ' Basic ' )]
3433 [ValidateSet (" Desc" , " Asc" )]
35- [string ]$OrderDirection = ' Desc' ,
34+ [string ] $OrderDirection = ' Desc' ,
3635
3736 # Hashtable containing machine field names and values returned must match exactly (will be combined with AND)
38- [parameter ()]
39- [hashtable ]$MatchExact ,
37+ [parameter (ParameterSetName = ' Basic ' )]
38+ [hashtable ] $MatchExact ,
4039
4140 # Hashtable containing machine field names and values returned rows must contain (will be combined with AND)
42- [parameter ()]
43- [hashtable ]$MatchContains
41+ [parameter (ParameterSetName = ' Basic' )]
42+ [hashtable ] $MatchContains ,
43+
44+ [parameter (ParameterSetName = ' Advanced' )]
45+ [System.Collections.ArrayList ] $Filter ,
46+
47+ [parameter (ParameterSetName = ' Advanced' )]
48+ [ValidateNotNullOrEmpty ()]
49+ [System.Collections.ArrayList ] $Order = @ (' opened_at' , ' desc' )
50+
4451 )
4552
46- Try {
53+ Write-Verbose (' {0} - {1}' -f $MyInvocation.MyCommand , $PSCmdlet.ParameterSetName )
54+
55+ if ( $PSCmdlet.ParameterSetName -eq ' Advanced' ) {
56+ if ( $Filter ) {
57+ $filterList = $Filter
58+ # see if we're working with 1 array or multidimensional array
59+ # we're looking for multidimensional so convert if not
60+ if ($Filter [0 ].GetType().Name -eq ' String' ) {
61+ $filterList = @ (, $Filter )
62+ }
63+
64+ $query = for ($i = 0 ; $i -lt $filterList.Count ; $i ++ ) {
65+ $thisFilter = $filterList [$i ]
66+
67+ # allow passing of string instead of array
68+ # useful for joins
69+ if ($thisFilter.GetType ().Name -eq ' String' ) {
70+ $thisFilter = @ (, $thisFilter )
71+ }
72+
73+ switch ($thisFilter.Count ) {
74+ 0 {
75+ # nothing to see here
76+ Continue
77+ }
78+
79+ 1 {
80+ # should be a join
81+
82+ switch ($thisFilter [0 ]) {
83+ ' and' {
84+ ' ^'
85+ }
86+
87+ ' or' {
88+ ' ^OR'
89+ }
90+
91+ ' group' {
92+ ' ^NQ'
93+ }
94+
95+ Default {
96+ throw " Unsupported join operator '$ ( $thisFilter [0 ]) '. 'and', 'or', and 'group' are supported."
97+ }
98+ }
99+
100+ # make sure we don't end on a join
101+ if ( $i -eq $filterList.Count - 1 ) {
102+ throw ' $Filter cannot end with a join'
103+ }
104+ }
105+
106+ 2 {
107+ # should be a non-value operator
108+ $thisOperator = $script :ServiceNowOperator | Where-Object { $_.Name -eq $thisFilter [1 ] }
109+ if ( -not $thisOperator ) {
110+ throw (' Operator '' {0}'' is not valid' -f $thisFilter [1 ])
111+ }
112+ if ( $thisOperator.RequiresValue ) {
113+ throw (' Value not provided, {0} {1} ?' -f $thisFilter [0 ], $thisOperator.QueryOperator )
114+ }
115+ ' {0}{1}' -f $thisFilter [0 ], $thisOperator.QueryOperator
116+ }
117+
118+ 3 {
119+ # should be key operator value
120+ $thisOperator = $script :ServiceNowOperator | Where-Object { $_.Name -eq $thisFilter [1 ] }
121+ if ( -not $thisOperator ) {
122+ throw (' Operator '' {0}'' is not valid' , $thisFilter [1 ])
123+ }
124+ ' {0}{1}{2}' -f $thisFilter [0 ], $thisOperator.QueryOperator , $thisFilter [2 ]
125+ }
126+
127+ Default {
128+ throw (' Too many items for {0}, see the help' -f $thisFilter [0 ])
129+ }
130+ }
131+ }
132+ }
133+
134+ # force query to an array in case we only got one item and its a string
135+ # otherwise below add to query won't work as expected
136+ $query = @ ($query )
137+
138+ if ($query ) {
139+ $query += ' ^'
140+ }
141+
142+ $orderList = $Order
143+ # see if we're working with 1 array or multidimensional array
144+ # we're looking for multidimensional so convert if not
145+ if ($Order [0 ].GetType().Name -eq ' String' ) {
146+ $orderList = @ (, $Order )
147+ }
148+
149+ $query += for ($i = 0 ; $i -lt $orderList.Count ; $i ++ ) {
150+ $thisOrder = $orderList [$i ]
151+ if ( $orderList.Count -gt 1 -and $i -gt 0 ) {
152+ ' ^'
153+ }
154+
155+ switch ($thisOrder.Count ) {
156+ 0 {
157+ # nothing to see here
158+ Continue
159+ }
160+
161+ 1 {
162+ # should be field, default to ascending
163+ ' ORDERBY'
164+ $thisOrder [0 ]
165+ }
166+
167+ 2 {
168+ switch ($thisOrder [1 ]) {
169+ ' asc' {
170+ ' ORDERBY'
171+ }
172+
173+ ' desc' {
174+ ' ORDERBYDESC'
175+ }
176+
177+ Default {
178+ throw " Invalid order direction '$_ '. Provide either 'asc' or 'desc'."
179+ }
180+ }
181+ $thisOrder [0 ]
182+ }
183+
184+ Default {
185+ throw (' Too many items for {0}, see the help' -f $thisOrder [0 ])
186+ }
187+ }
188+ }
189+
190+ $query -join ' '
191+
192+ } else {
193+ # Basic parameter set
194+
47195 # Create StringBuilder
48196 $Query = New-Object System.Text.StringBuilder
49197
50198 # Start the query off with a order direction
51- $Order = Switch ($OrderDirection ) {
52- ' Asc' { ' ORDERBY' ; break }
53- Default {' ORDERBYDESC' }
199+ $direction = Switch ($OrderDirection ) {
200+ ' Asc' { ' ORDERBY' ; break }
201+ Default { ' ORDERBYDESC' }
54202 }
55- [void ]$Query.Append ($Order )
203+ [void ]$Query.Append ($direction )
56204
57205 # Add OrderBy
58206 [void ]$Query.Append ($OrderBy )
@@ -76,7 +224,4 @@ function New-ServiceNowQuery {
76224 # Output StringBuilder to string
77225 $Query.ToString ()
78226 }
79- Catch {
80- Write-Error $PSItem
81- }
82- }
227+ }
0 commit comments