Skip to content

Commit 180cfa7

Browse files
authored
Merge pull request #59 from Sam-Martin/development
v1.2.4 - New-ServiceNowQuery Improvements
2 parents 00b942c + 643b3ef commit 180cfa7

File tree

3 files changed

+65
-26
lines changed

3 files changed

+65
-26
lines changed

Readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# ServiceNow
22

3-
[![GitHub release](https://img.shields.io/github/release/Sam-Martin/servicenow-powershell.svg)](https://github.com/Sam-Martin/servicenow-powershell/releases/latest) [![GitHub license](https://img.shields.io/github/license/Sam-Martin/servicenow-powershell.svg)](LICENSE) ![Test Coverage](https://img.shields.io/badge/coverage-80%25-yellow.svg)
3+
[![GitHub release](https://img.shields.io/github/release/Sam-Martin/servicenow-powershell.svg)](https://github.com/Sam-Martin/servicenow-powershell/releases/latest) [![GitHub license](https://img.shields.io/github/license/Sam-Martin/servicenow-powershell.svg)](LICENSE) ![Test Coverage](https://img.shields.io/badge/coverage-79%25-yellow.svg)
44

55
This PowerShell module provides a series of cmdlets for interacting with the [ServiceNow REST API](http://wiki.servicenow.com/index.php?title=REST_API), performed by wrapping `Invoke-RestMethod` for the API calls.
66

@@ -44,7 +44,7 @@ Once you've done this, all the cmdlets will be at your disposal, you can see a f
4444
Set-ServiceNowAuth -url InstanceName.service-now.com -Credentials (Get-Credential)
4545
```
4646

47-
The URL should be the instance name portion of the FQDN for your instance. For if you browse to `https://yourinstance.service-now.com` the URL required for the module is `yourinstance.service-now.com`.
47+
The URL should be the instance name portion of the FQDN for your instance. If you browse to `https://yourinstance.service-now.com` the URL required for the module is `yourinstance.service-now.com`.
4848

4949
### Example - Retrieving an Incident Containing the Word 'PowerShell'
5050

@@ -76,7 +76,7 @@ $IncidentParams = @{Caller = "UserName"
7676
Description = "This incident was created from Powershell"
7777
CustomFields = @{u_service = "MyService"
7878
u_incident_type = "Request"}
79-
}
79+
}
8080
New-ServiceNowIncident @Params
8181
```
8282

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,39 @@
1-
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','')]
23+
24+
[CmdletBinding()]
25+
[OutputType([System.String])]
226

327
param(
428
# Machine name of the field to order by
529
[parameter(mandatory=$false)]
630
[string]$OrderBy='opened_at',
7-
31+
832
# Direction of ordering (Desc/Asc)
933
[parameter(mandatory=$false)]
1034
[ValidateSet("Desc", "Asc")]
1135
[string]$OrderDirection='Desc',
12-
36+
1337
# Hashtable containing machine field names and values returned must match exactly (will be combined with AND)
1438
[parameter(mandatory=$false)]
1539
[hashtable]$MatchExact,
@@ -18,28 +42,41 @@ function New-ServiceNowQuery{
1842
[parameter(mandatory=$false)]
1943
[hashtable]$MatchContains
2044
)
21-
# Start the query off with a order direction
22-
$Query = '';
23-
if($OrderDirection -eq 'Asc'){
24-
$Query += 'ORDERBY'
25-
}else{
26-
$Query += 'ORDERBYDESC'
27-
}
28-
$Query +="$OrderBy"
2945

30-
# Build the exact matches into the query
31-
if($MatchExact){
32-
foreach($Field in $MatchExact.keys){
33-
$Query += "^$Field="+$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'}
3454
}
35-
}
55+
[void]$Query.Append($Order)
56+
57+
# Add OrderBy
58+
[void]$Query.Append($OrderBy)
3659

37-
# Add the values which given fields should contain
38-
if($MatchContains){
39-
foreach($Field in $MatchContains.keys){
40-
$Query += "^$($Field)LIKE"+$MatchContains.$Field
60+
# Build the exact matches into the query
61+
If ($MatchExact) {
62+
ForEach ($Field in $MatchExact.keys) {
63+
$ExactString = "^{0}={1}" -f $Field.ToString().ToLower(), ($MatchExact.$Field)
64+
[void]$Query.Append($ExactString)
65+
}
4166
}
42-
}
4367

44-
return $Query
68+
# Add the values which given fields should contain
69+
If ($MatchContains) {
70+
ForEach ($Field in $MatchContains.keys) {
71+
$ContainsString = "^{0}LIKE{1}" -f $Field.ToString().ToLower(), ($MatchContains.$Field)
72+
[void]$Query.Append($ContainsString)
73+
}
74+
}
75+
76+
# Output StringBuilder to string
77+
$Query.ToString()
78+
}
79+
Catch {
80+
Write-Error $PSItem
81+
}
4582
}

ServiceNow/ServiceNow.psd1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = 'ServiceNow.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '1.2.3'
15+
ModuleVersion = '1.2.4'
1616

1717
# ID used to uniquely identify this module
1818
GUID = 'b90d67da-f8d0-4406-ad74-89d169cd0633'
@@ -105,3 +105,5 @@ PrivateData = @{
105105

106106

107107

108+
109+

0 commit comments

Comments
 (0)