1- <#
2- . SYNOPSIS
3- Build query string for api call
4- . DESCRIPTION
5- Build query string for api call
6- . EXAMPLE
7- New-ServiceNowQuery -MatchExact @{field_name=value}
8- Get query string where field name exactly matches the value
9- . EXAMPLE
10- New-ServiceNowQuery -MatchContains @{field_name=value}
11- Get query string where field name contains the value
12- . INPUTS
13- None
14- . OUTPUTS
15- String
16- #>
17- function New-ServiceNowQuery {
1+ 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+
21+ # This function doesn't change state. Doesn't justify ShouldProcess functionality
22+ [System.Diagnostics.CodeAnalysis.SuppressMessage (' PSUseShouldProcessForStateChangingFunctions' , ' ' )]
1823
1924 [CmdletBinding ()]
2025 [OutputType ([System.String ])]
@@ -37,28 +42,39 @@ function New-ServiceNowQuery{
3742 [parameter (mandatory = $false )]
3843 [hashtable ]$MatchContains
3944 )
40- # Start the query off with a order direction
41- $Query = ' ' ;
42- if ($OrderDirection -eq ' Asc' ){
43- $Query += ' ORDERBY'
44- }else {
45- $Query += ' ORDERBYDESC'
46- }
47- $Query += " $OrderBy "
4845
49- # Build the exact matches into the query
50- if ($MatchExact ){
51- foreach ($Field in $MatchExact.keys ){
52- $Query += " ^{0}={1}" -f $Field.ToString ().ToLower(), ($MatchExact .$Field )
46+ Try {
47+ # Create StringBuilder
48+ $Query = New-Object System.Text.StringBuilder
49+
50+ # Start the query off with a order direction
51+ $Order = Switch ($OrderDirection ) {
52+ ' Asc' {' ORDERBY' }
53+ Default {' ORDERBYDESC' }
5354 }
54- }
55+ [ void ] $Query .Append ( $Order )
5556
56- # Add the values which given fields should contain
57- if ($MatchContains ){
58- foreach ($Field in $MatchContains.keys ){
59- $Query += " ^{0}LIKE{1}" -f $Field.ToString ().ToLower(), ($MatchContains .$Field )
57+ # Build the exact matches into the query
58+ If ($MatchExact ) {
59+ $Match = ForEach ($Field in $MatchExact.keys ) {
60+ " ^{0}={1}" -f $Field.ToString ().ToLower(), ($MatchExact .$Field )
61+ }
62+ }
63+
64+ # Add the values which given fields should contain
65+ If ($MatchContains ) {
66+ $Match = ForEach ($Field in $MatchContains.keys ) {
67+ " ^{0}LIKE{1}" -f $Field.ToString ().ToLower(), ($MatchContains .$Field )
68+ }
6069 }
61- }
6270
63- return $Query
71+ # Append Match (Exact or Contains)
72+ [void ]$Query.Append ($Match )
73+
74+ # Output StringBuilder to string
75+ $Query.ToString ()
76+ }
77+ Catch {
78+ Write-Error $PSItem
79+ }
6480}
0 commit comments