Skip to content

Commit 848423d

Browse files
authored
Merge pull request #38 from Sam-Martin/development
v1.1.0 - Get-ServiceNowRequest & Various Minor Updates
2 parents 1b3ca3d + 855c9ee commit 848423d

9 files changed

+215
-25
lines changed

Readme.md

+2-2
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-73%25-orange.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-74%25-orange.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://myinstance.service-now.com` the URL required for the module is `myinstance.service-now.com`.
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`.
4848

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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
function Get-ServiceNowRequest {
2+
param(
3+
# Machine name of the field to order by
4+
[parameter(mandatory = $false)]
5+
[parameter(ParameterSetName = 'SpecifyConnectionFields')]
6+
[parameter(ParameterSetName = 'UseConnectionObject')]
7+
[parameter(ParameterSetName = 'SetGlobalAuth')]
8+
[string]$OrderBy = 'opened_at',
9+
10+
# Direction of ordering (Desc/Asc)
11+
[parameter(mandatory = $false)]
12+
[parameter(ParameterSetName = 'SpecifyConnectionFields')]
13+
[parameter(ParameterSetName = 'UseConnectionObject')]
14+
[parameter(ParameterSetName = 'SetGlobalAuth')]
15+
[ValidateSet("Desc", "Asc")]
16+
[string]$OrderDirection = 'Desc',
17+
18+
# Maximum number of records to return
19+
[parameter(mandatory = $false)]
20+
[parameter(ParameterSetName = 'SpecifyConnectionFields')]
21+
[parameter(ParameterSetName = 'UseConnectionObject')]
22+
[parameter(ParameterSetName = 'SetGlobalAuth')]
23+
[int]$Limit = 10,
24+
25+
# Hashtable containing machine field names and values returned must match exactly (will be combined with AND)
26+
[parameter(mandatory = $false)]
27+
[parameter(ParameterSetName = 'SpecifyConnectionFields')]
28+
[parameter(ParameterSetName = 'UseConnectionObject')]
29+
[parameter(ParameterSetName = 'SetGlobalAuth')]
30+
[hashtable]$MatchExact = @{},
31+
32+
# Hashtable containing machine field names and values returned rows must contain (will be combined with AND)
33+
[parameter(mandatory = $false)]
34+
[parameter(ParameterSetName = 'SpecifyConnectionFields')]
35+
[parameter(ParameterSetName = 'UseConnectionObject')]
36+
[parameter(ParameterSetName = 'SetGlobalAuth')]
37+
[hashtable]$MatchContains = @{},
38+
39+
# Whether or not to show human readable display values instead of machine values
40+
[parameter(mandatory = $false)]
41+
[parameter(ParameterSetName = 'SpecifyConnectionFields')]
42+
[parameter(ParameterSetName = 'UseConnectionObject')]
43+
[parameter(ParameterSetName = 'SetGlobalAuth')]
44+
[ValidateSet("true", "false", "all")]
45+
[string]$DisplayValues = 'true',
46+
47+
[Parameter(ParameterSetName = 'SpecifyConnectionFields', Mandatory = $True)]
48+
[ValidateNotNullOrEmpty()]
49+
[PSCredential]
50+
$ServiceNowCredential,
51+
52+
[Parameter(ParameterSetName = 'SpecifyConnectionFields', Mandatory = $True)]
53+
[ValidateNotNullOrEmpty()]
54+
[string]
55+
$ServiceNowURL,
56+
57+
[Parameter(ParameterSetName = 'UseConnectionObject', Mandatory = $True)]
58+
[ValidateNotNullOrEmpty()]
59+
[Hashtable]
60+
$Connection
61+
)
62+
63+
# Query Splat
64+
$newServiceNowQuerySplat = @{
65+
OrderBy = $OrderBy
66+
MatchExact = $MatchExact
67+
OrderDirection = $OrderDirection
68+
MatchContains = $MatchContains
69+
}
70+
$Query = New-ServiceNowQuery @newServiceNowQuerySplat
71+
72+
# Table Splat
73+
$getServiceNowTableSplat = @{
74+
Table = 'sc_request'
75+
Query = $Query
76+
Limit = $Limit
77+
DisplayValues = $DisplayValues
78+
}
79+
80+
# Update the Table Splat if the parameters have values
81+
if ($null -ne $PSBoundParameters.Connection) {
82+
$getServiceNowTableSplat.Add('Connection', $Connection)
83+
}
84+
elseif ($null -ne $PSBoundParameters.ServiceNowCredential -and $null -ne $PSBoundParameters.ServiceNowURL) {
85+
$getServiceNowTableSplat.Add('ServiceNowCredential', $ServiceNowCredential)
86+
$getServiceNowTableSplat.Add('ServiceNowURL', $ServiceNowURL)
87+
}
88+
89+
# Perform query and return each object in the format.ps1xml format
90+
$Result = Get-ServiceNowTable @getServiceNowTableSplat
91+
$Result | ForEach-Object {$_.PSObject.TypeNames.Insert(0, "ServiceNow.Request")}
92+
$Result
93+
}
+43-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,45 @@
1-
function Set-ServiceNowAuth{
2-
param(
3-
[parameter(mandatory=$true)]
4-
[string]$url,
5-
6-
[parameter(mandatory=$true)]
7-
[System.Management.Automation.PSCredential]$Credentials
1+
<#
2+
.SYNOPSIS
3+
Set your Service-Now authentication credentials
4+
5+
.DESCRIPTION
6+
This cmdlet will set your Service-Now authentication credentials which will enable you to interact with Service-Now using the other cmdlets in the module
7+
8+
.PARAMETER Url
9+
The URL of your Service-Now instance
10+
11+
.PARAMETER Credentials
12+
Credentials to authenticate you to the Service-Now instance provided in the Url parameter
13+
14+
.EXAMPLE
15+
Set-ServiceNowAuth -Url tenant.service-now.com
16+
17+
.NOTES
18+
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
19+
#>
20+
function Set-ServiceNowAuth {
21+
[CmdletBinding()]
22+
Param (
23+
[Parameter(Mandatory = $true)]
24+
[ValidateNotNullOrEmpty()]
25+
[ValidateScript( {
26+
if ($_ -match '^\w+\..*\.\w+') {
27+
$true
28+
}
29+
else {
30+
Throw "The expected URL format is tenant.domain.com"
31+
}
32+
})]
33+
[string]
34+
$Url,
35+
36+
[Parameter(Mandatory = $true)]
37+
[ValidateNotNullOrEmpty()]
38+
[System.Management.Automation.PSCredential]
39+
$Credentials
840
)
9-
$Global:ServiceNowURL = 'https://' + $url
10-
$Global:ServiceNowRESTURL = $ServiceNowURL + '/api/now/v1'
11-
$Global:ServiceNowCredentials = $credentials
12-
return $true;
41+
$Global:serviceNowUrl = 'https://' + $Url
42+
$Global:serviceNowRestUrl = $serviceNowUrl + '/api/now/v1'
43+
$Global:serviceNowCredentials = $Credentials
44+
return $true
1345
}

ServiceNow/Public/Update-ServiceNowChangeRequest.ps1

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
function Update-ServiceNowChangeRequest
66
{
77
Param(
8-
# sys_id of the caller of the incident (user Get-ServiceNowUser to retrieve this)
8+
# sys_id of the caller of the incident (use Get-ServiceNowUser to retrieve this)
99
[parameter(Mandatory=$true)]
1010
[parameter(ParameterSetName='SpecifyConnectionFields')]
1111
[parameter(ParameterSetName='UseConnectionObject')]
@@ -50,4 +50,4 @@ function Update-ServiceNowChangeRequest
5050
}
5151

5252
Update-ServiceNowTableEntry @updateServiceNowTableEntrySplat
53-
}
53+
}

ServiceNow/Public/Update-ServiceNowIncident.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function Update-ServiceNowIncident {
22
Param
3-
( # sys_id of the caller of the incident (user Get-ServiceNowUser to retrieve this)
3+
( # sys_id of the caller of the incident (use Get-ServiceNowUser to retrieve this)
44
[parameter(mandatory=$true)]
55
[parameter(ParameterSetName='SpecifyConnectionFields', mandatory=$true)]
66
[parameter(ParameterSetName='UseConnectionObject', mandatory=$true)]

ServiceNow/Public/Update-ServiceNowTableEntry.ps1

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function Update-ServiceNowTableEntry{
2525
[ValidateNotNullOrEmpty()]
2626
[string]$ServiceNowURL,
2727

28-
#Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
28+
# Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
2929
[Parameter(ParameterSetName='UseConnectionObject', Mandatory=$True)]
3030
[ValidateNotNullOrEmpty()]
3131
[Hashtable]$Connection,
@@ -38,7 +38,7 @@ function Update-ServiceNowTableEntry{
3838
[hashtable]$Values
3939
)
4040

41-
#Get credential and ServiceNow REST URL
41+
# Get credential and ServiceNow REST URL
4242
if ($Connection -ne $null)
4343
{
4444
$SecurePassword = ConvertTo-SecureString $Connection.Password -AsPlainText -Force
@@ -62,10 +62,10 @@ function Update-ServiceNowTableEntry{
6262

6363
$Body = $Values | ConvertTo-Json
6464

65-
#Convert to UTF8 array to support special chars such as the danish "�","�","�"
65+
# Convert to UTF8 array to support special chars such as the danish "�","�","�"
6666
$utf8Bytes = [System.Text.Encoding]::UTf8.GetBytes($Body)
6767

6868
# Fire and return
6969
$Uri = $ServiceNowURL + "/table/$Table/$SysID"
7070
return (Invoke-RestMethod -Uri $uri -Method Patch -Credential $ServiceNowCredential -Body $utf8Bytes -ContentType "application/json").result
71-
}
71+
}

ServiceNow/ServiceNow.format.ps1xml

+61-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,66 @@
7070
</TableRowEntries>
7171
</TableControl>
7272
</View>
73+
<View>
74+
<Name>ServiceNow.Request</Name>
75+
<ViewSelectedBy>
76+
<TypeName>ServiceNow.Request</TypeName>
77+
</ViewSelectedBy>
78+
<TableControl>
79+
<TableHeaders>
80+
<TableColumnHeader>
81+
<Label>number</Label>
82+
<Width>10</Width>
83+
</TableColumnHeader>
84+
<TableColumnHeader>
85+
<Label>short_description</Label>
86+
<Width>25</Width>
87+
</TableColumnHeader>
88+
<TableColumnHeader>
89+
<Label>state</Label>
90+
<Width>8</Width>
91+
</TableColumnHeader>
92+
<TableColumnHeader>
93+
<Label>assigned_to</Label>
94+
<Width>15</Width>
95+
</TableColumnHeader>
96+
<TableColumnHeader>
97+
<Label>approval</Label>
98+
<Width>10</Width>
99+
</TableColumnHeader>
100+
<TableColumnHeader>
101+
<Label>opened_at</Label>
102+
<Width>21</Width>
103+
</TableColumnHeader>
104+
</TableHeaders>
105+
<TableRowEntries>
106+
<TableRowEntry>
107+
<TableColumnItems>
108+
<TableColumnItem>
109+
<PropertyName>number</PropertyName>
110+
</TableColumnItem>
111+
<TableColumnItem>
112+
<PropertyName>short_description</PropertyName>
113+
</TableColumnItem>
114+
<TableColumnItem>
115+
<PropertyName>state</PropertyName>
116+
</TableColumnItem>
117+
<TableColumnItem>
118+
<ScriptBlock>
119+
$_.assigned_to.display_value
120+
</ScriptBlock>
121+
</TableColumnItem>
122+
<TableColumnItem>
123+
<PropertyName>approval</PropertyName>
124+
</TableColumnItem>
125+
<TableColumnItem>
126+
<PropertyName>opened_at</PropertyName>
127+
</TableColumnItem>
128+
</TableColumnItems>
129+
</TableRowEntry>
130+
</TableRowEntries>
131+
</TableControl>
132+
</View>
73133
<View>
74134
<Name>ServiceNow.ConfigurationItem</Name>
75135
<ViewSelectedBy>
@@ -189,4 +249,4 @@
189249
</TableControl>
190250
</View>
191251
</ViewDefinitions>
192-
</Configuration>
252+
</Configuration>

ServiceNow/ServiceNow.psd1

+2-2
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.0.1'
15+
ModuleVersion = '1.1.0'
1616

1717
# ID used to uniquely identify this module
1818
GUID = 'b90d67da-f8d0-4406-ad74-89d169cd0633'
@@ -66,7 +66,7 @@ FormatsToProcess = @('ServiceNow.format.ps1xml')
6666
NestedModules = @()
6767

6868
# Functions to export from this module
69-
FunctionsToExport = @('Get-ServiceNowChangeRequest','Get-ServiceNowConfigurationItem','Get-ServiceNowIncident','Get-ServiceNowTable','Get-ServiceNowUser','Get-ServiceNowUserGroup','New-ServiceNowIncident','New-ServiceNowQuery','New-ServiceNowTableEntry','Remove-ServiceNowAuth','Remove-ServiceNowTableEntry','Set-ServiceNowAuth','Test-ServiceNowAuthIsSet','Update-ServiceNowChangeRequest','Update-ServiceNowIncident','Update-ServiceNowTableEntry')
69+
FunctionsToExport = @('Get-ServiceNowChangeRequest','Get-ServiceNowConfigurationItem','Get-ServiceNowIncident','Get-ServiceNowRequest','Get-ServiceNowTable','Get-ServiceNowUser','Get-ServiceNowUserGroup','New-ServiceNowIncident','New-ServiceNowQuery','New-ServiceNowTableEntry','Remove-ServiceNowAuth','Remove-ServiceNowTableEntry','Set-ServiceNowAuth','Test-ServiceNowAuthIsSet','Update-ServiceNowChangeRequest','Update-ServiceNowIncident','Update-ServiceNowTableEntry')
7070

7171
# List of all modules packaged with this module
7272
# ModuleList = @()

Tests/ServiceNow.Tests.ps1

+7-2
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,17 @@ Describe "ServiceNow-Module" {
6767

6868
It "Get-ServiceNowTable works" {
6969
# There should be one or more incidents returned
70-
(Get-ServiceNowTable -Table 'incident' -Query 'ORDERBYDESCopened_at').Count -gt 0 | Should -Match $true
70+
([array](Get-ServiceNowTable -Table 'incident' -Query 'ORDERBYDESCopened_at')).Count -gt 0 | Should -Match $true
7171
}
7272

7373
It "Get-ServiceNowIncident works" {
7474
# There should be one or more incidents returned
75-
(Get-ServiceNowIncident).Count -gt 0 | Should -Match $true
75+
([array](Get-ServiceNowIncident)).count -gt 0 | Should -Match $true
76+
}
77+
78+
It "Get-ServiceNowRequest works" {
79+
# There should be one or more incidents returned
80+
([array](Get-ServiceNowRequest)).count -gt 0 | Should -Match $true
7681
}
7782

7883
It "Update-ServiceNowIncident works" {

0 commit comments

Comments
 (0)