Skip to content

Commit 1e6ed2c

Browse files
authored
Merge pull request #9 from Sam-Martin/development
v1.13
2 parents 10945dc + 13fac00 commit 1e6ed2c

7 files changed

+235
-11
lines changed

MAKE.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ $PackageFilePatternExclusions = @(
6666

6767
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
6868

69-
$Version = "0.1.12"
69+
$Version = "0.1.13"
7070
$ModuleName = "PSServiceNow"
7171
$PackageName = "$ModuleName-v$($version).zip";
7272

PSServiceNow-Changes.psm1

+51
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,54 @@ function Get-ServiceNowChangeRequest {
7878
return $private:result
7979
}
8080

81+
82+
<#
83+
.EXAMPLE
84+
Update-ServiceNowChangeRequest -Values @{ 'state' = 3 } -SysId <sysid>
85+
#>
86+
function Update-ServiceNowChangeRequest
87+
{
88+
Param( # sys_id of the caller of the incident (user Get-ServiceNowUser to retrieve this)
89+
[parameter(mandatory=$true)]
90+
[parameter(ParameterSetName='SpecifyConnectionFields', mandatory=$true)]
91+
[parameter(ParameterSetName='UseConnectionObject', mandatory=$true)]
92+
[parameter(ParameterSetName='SetGlobalAuth', mandatory=$true)]
93+
[string]$SysId,
94+
95+
# Hashtable of values to use as the record's properties
96+
[parameter(mandatory=$true)]
97+
[hashtable]$Values,
98+
99+
# Credential used to authenticate to ServiceNow
100+
[Parameter(ParameterSetName='SpecifyConnectionFields', Mandatory=$True)]
101+
[ValidateNotNullOrEmpty()]
102+
[PSCredential]
103+
$ServiceNowCredential,
104+
105+
# The URL for the ServiceNow instance being used (eg: instancename.service-now.com)
106+
[Parameter(ParameterSetName='SpecifyConnectionFields', Mandatory=$True)]
107+
[ValidateNotNullOrEmpty()]
108+
[string]
109+
$ServiceNowURL,
110+
111+
#Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
112+
[Parameter(ParameterSetName='UseConnectionObject', Mandatory=$True)]
113+
[ValidateNotNullOrEmpty()]
114+
[Hashtable]
115+
$Connection
116+
)
117+
118+
if ($Connection -ne $null)
119+
{
120+
Update-ServiceNowTableEntry -Table 'change_request' -Values $Values -Connection $Connection -SysId $SysId
121+
}
122+
elseif ($ServiceNowCredential -ne $null -and $ServiceNowURL -ne $null)
123+
{
124+
Update-ServiceNowTableEntry -Table 'change_request' -Values $Values -ServiceNowCredential $ServiceNowCredential -ServiceNowURL $ServiceNowURL -SysId $SysId
125+
}
126+
else
127+
{
128+
Update-ServiceNowTableEntry -Table 'change_request' -Values $Values -SysId $SysId
129+
}
130+
}
131+

PSServiceNow-Incidents.psm1

+51-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function Get-ServiceNowIncident{
9393

9494
# Set the default property set for the table view
9595
$DefaultProperties = @('number', 'short_description', 'opened_at')
96-
$DefaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet(DefaultDisplayPropertySet,[string[]]$DefaultProperties)
96+
$DefaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet('DefaultDisplayPropertySet',[string[]]$DefaultProperties)
9797
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($DefaultDisplayPropertySet)
9898
$Result | Add-Member MemberSet PSStandardMembers $PSStandardMembers
9999

@@ -225,3 +225,53 @@ function New-ServiceNowIncident{
225225

226226
}
227227

228+
229+
<#
230+
.EXAMPLE
231+
Update-ServiceNowIncident-Values @{ 'short_description' = 'updated description'} -SysId <sysid>
232+
#>
233+
function Update-ServiceNowIncident
234+
{
235+
Param( # sys_id of the caller of the incident (user Get-ServiceNowUser to retrieve this)
236+
[parameter(mandatory=$true)]
237+
[parameter(ParameterSetName='SpecifyConnectionFields', mandatory=$true)]
238+
[parameter(ParameterSetName='UseConnectionObject', mandatory=$true)]
239+
[parameter(ParameterSetName='SetGlobalAuth', mandatory=$true)]
240+
[string]$SysId,
241+
242+
# Hashtable of values to use as the record's properties
243+
[parameter(mandatory=$true)]
244+
[hashtable]$Values,
245+
246+
# Credential used to authenticate to ServiceNow
247+
[Parameter(ParameterSetName='SpecifyConnectionFields', Mandatory=$True)]
248+
[ValidateNotNullOrEmpty()]
249+
[PSCredential]
250+
$ServiceNowCredential,
251+
252+
# The URL for the ServiceNow instance being used (eg: instancename.service-now.com)
253+
[Parameter(ParameterSetName='SpecifyConnectionFields', Mandatory=$True)]
254+
[ValidateNotNullOrEmpty()]
255+
[string]
256+
$ServiceNowURL,
257+
258+
#Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
259+
[Parameter(ParameterSetName='UseConnectionObject', Mandatory=$True)]
260+
[ValidateNotNullOrEmpty()]
261+
[Hashtable]
262+
$Connection
263+
)
264+
265+
if ($Connection -ne $null)
266+
{
267+
Update-ServiceNowTableEntry -Table 'incident' -Values $Values -Connection $Connection -SysId $SysId
268+
}
269+
elseif ($ServiceNowCredential -ne $null -and $ServiceNowURL -ne $null)
270+
{
271+
Update-ServiceNowTableEntry -Table 'incident' -Values $Values -ServiceNowCredential $ServiceNowCredential -ServiceNowURL $ServiceNowURL -SysId $SysId
272+
}
273+
else
274+
{
275+
Update-ServiceNowTableEntry -Table 'incident' -Values $Values -SysId $SysId
276+
}
277+
}

PSServiceNow-Tables.psm1

+77
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,81 @@ function Remove-ServiceNowTableEntry{
216216
# Fire and return
217217
$Uri = $ServiceNowURL + "/table/$Table/$SysID"
218218
return (Invoke-RestMethod -Uri $uri -Method Delete -Credential $ServiceNowCredential -Body $Body -ContentType "application/json").result
219+
}
220+
221+
222+
function Update-ServiceNowTableEntry{
223+
[CmdletBinding(ConfirmImpact='High')]
224+
Param(
225+
# sys_id of the entry we're deleting
226+
[parameter(mandatory=$true)]
227+
[parameter(ParameterSetName='SpecifyConnectionFields')]
228+
[parameter(ParameterSetName='UseConnectionObject')]
229+
[parameter(ParameterSetName='SetGlobalAuth')]
230+
[string]$SysId,
231+
232+
# Table containing the entry we're deleting
233+
[parameter(mandatory=$true)]
234+
[parameter(ParameterSetName='SpecifyConnectionFields')]
235+
[parameter(ParameterSetName='UseConnectionObject')]
236+
[parameter(ParameterSetName='SetGlobalAuth')]
237+
[string]$Table,
238+
239+
# Credential used to authenticate to ServiceNow
240+
[Parameter(ParameterSetName='SpecifyConnectionFields', Mandatory=$True)]
241+
[ValidateNotNullOrEmpty()]
242+
[PSCredential]
243+
$ServiceNowCredential,
244+
245+
# The URL for the ServiceNow instance being used
246+
[Parameter(ParameterSetName='SpecifyConnectionFields', Mandatory=$True)]
247+
[ValidateNotNullOrEmpty()]
248+
[string]
249+
$ServiceNowURL,
250+
251+
#Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
252+
[Parameter(ParameterSetName='UseConnectionObject', Mandatory=$True)]
253+
[ValidateNotNullOrEmpty()]
254+
[Hashtable]
255+
$Connection,
256+
257+
# Hashtable of values to use as the record's properties
258+
[parameter(mandatory=$false)]
259+
[parameter(ParameterSetName='SpecifyConnectionFields')]
260+
[parameter(ParameterSetName='UseConnectionObject')]
261+
[parameter(ParameterSetName='SetGlobalAuth')]
262+
[hashtable]$Values
263+
264+
)
265+
266+
#Get credential and ServiceNow REST URL
267+
if ($Connection -ne $null)
268+
{
269+
$SecurePassword = ConvertTo-SecureString $Connection.Password -AsPlainText -Force
270+
$ServiceNowCredential = New-Object System.Management.Automation.PSCredential ($Connection.Username, $SecurePassword)
271+
$ServiceNowURL = 'https://' + $Connection.ServiceNowUri + '/api/now/v1'
272+
273+
}
274+
elseif ($ServiceNowCredential -ne $null -and $ServiceNowURL -ne $null)
275+
{
276+
$ServiceNowURL = 'https://' + $ServiceNowURL + '/api/now/v1'
277+
}
278+
elseif((Test-ServiceNowAuthIsSet))
279+
{
280+
$ServiceNowCredential = $Global:ServiceNowCredentials
281+
$ServiceNowURL = $global:ServiceNowRESTURL
282+
}
283+
else
284+
{
285+
throw "Exception: You must do one of the following to authenticate: `n 1. Call the Set-ServiceNowAuth cmdlet `n 2. Pass in an Azure Automation connection object `n 3. Pass in an endpoint and credential"
286+
}
287+
288+
$Body = $Values | ConvertTo-Json;
289+
290+
#Convert to UTF8 array to support special chars such as the danish "�","�","�"
291+
$utf8Bytes = [System.Text.Encoding]::UTf8.GetBytes($Body)
292+
293+
# Fire and return
294+
$Uri = $ServiceNowURL + "/table/$Table/$SysID"
295+
return (Invoke-RestMethod -Uri $uri -Method Patch -Credential $ServiceNowCredential -Body $utf8Bytes -ContentType "application/json").result
219296
}

PSServiceNow.Tests.ps1

+26-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Remove-Module PSServiceNow -ErrorAction SilentlyContinue
2828
Import-Module $here\PSServiceNow.psd1
2929

3030
Describe "ServiceNow-Module" {
31-
31+
3232
It "Set-ServiceNowAuth works" {
3333
Set-ServiceNowAuth -url $defaults.ServiceNowURL -Credentials $defaults.Creds | Should be $true
3434
}
@@ -40,7 +40,7 @@ Describe "ServiceNow-Module" {
4040
-Comment "Comment" -ConfigurationItem $defaults.TestConfigurationItem `
4141
-Caller $defaults.TestUser `
4242

43-
$TestTicket.short_description | Should be "Testing with Pester"
43+
$TestTicket.short_description | Should be "Testing with Pester"
4444
}
4545

4646
It "Get-ServiceNowTable works" {
@@ -53,7 +53,29 @@ Describe "ServiceNow-Module" {
5353
(Get-ServiceNowIncident).Count -gt 0 | Should Match $true
5454
}
5555

56-
It "Get-ServiceNowUserGroup works" {
56+
It "Update-ServiceNowIncident works" {
57+
$TestTicket = New-ServiceNowIncident -ShortDescription "Testing Ticket Update with Pester" `
58+
-Description "Long description" -AssignmentGroup $defaults.TestUserGroup `
59+
-Category $defaults.TestCategory -SubCategory $Defaults.TestSubcategory `
60+
-Comment "Comment" -ConfigurationItem $defaults.TestConfigurationItem `
61+
-Caller $defaults.TestUser `
62+
63+
$TestTicket.short_description | Should be "Testing Ticket Update with Pester"
64+
65+
$Values =
66+
@{
67+
'short_description' = 'Ticket Updated with Pester'
68+
'description' = 'Even Longer Description'
69+
}
70+
71+
Update-ServiceNowIncident -SysId $TestTicket.sys_id -Values $Values
72+
73+
$TestTicket = Get-ServiceNowIncident -MatchExact @{sys_id=$TestTicket.sys_id}
74+
$TestTicket.short_description | Should be "Ticket Updated with Pester"
75+
$TestTicket.description | Should be "Even Longer Description"
76+
}
77+
78+
It "Get-ServiceNowUserGroup works" {
5779
# There should be one or more user groups returned
5880
(Get-ServiceNowUserGroup).Count -gt 0 | Should Match $true
5981
}
@@ -68,7 +90,7 @@ Describe "ServiceNow-Module" {
6890
(Get-ServiceNowConfigurationItem).Count -gt 0 | Should Match $true
6991
}
7092

71-
It "Get-ServiceNowChangeRequest works" {
93+
It "Get-ServiceNowChangeRequest works" {
7294
(Get-ServiceNowChangeRequest).Count -gt 0 | Should Match $true
7395
}
7496
}

PSServiceNow.psd1

+18-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = 'PSServiceNow.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '0.1.12'
15+
ModuleVersion = '0.1.13'
1616

1717
# ID used to uniquely identify this module
1818
GUID = 'b90d67da-f8d0-4406-ad74-89d169cd0633'
@@ -80,8 +80,23 @@ FunctionsToExport = '*'
8080
# List of all files packaged with this module
8181
# FileList = @()
8282

83-
# Private data to pass to the module specified in RootModule/ModuleToProcess
84-
# PrivateData = ''
83+
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
84+
PrivateData = @{
85+
86+
PSData = @{
87+
88+
# Tags applied to this module. These help with module discovery in online galleries.
89+
Tags = @('Azure','Automation','ServiceNow')
90+
91+
# A URL to the license for this module.
92+
LicenseUri = 'https://github.com/Sam-Martin/servicenow-powershell/blob/master/LICENSE'
93+
94+
# A URL to the main website for this project.
95+
ProjectUri = 'https://github.com/Sam-Martin/servicenow-powershell'
96+
97+
} # End of PSData hashtable
98+
99+
} # End of PrivateData hashtable
85100

86101
# HelpInfo URI of this module
87102
# HelpInfoURI = 'https://github.com/Sam-Martin/servicenow-powershell'

Readme.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# PSServiceNow
2-
[![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-70%25-yellowgreen.svg)
2+
[![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-68%25-yellowgreen.svg)
33
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.
4-
**IMPORTANT:** Neither this module, nor its creator are in any way affiliated with ServiceNow.
4+
**IMPORTANT:** Neither this module nor its creator are in any way affiliated with ServiceNow.
55

66
## Requirements
77
Requires PowerShell 3.0 or above as this is when `Invoke-RestMethod` was introduced.
@@ -24,6 +24,12 @@ Import-Module PSServiceNow
2424
Get-ServiceNowIncident -MatchContains @{short_description='PowerShell'} -ServiceNowCredential $PSCredential -ServiceNowURL $ServiceNowURL
2525
```
2626

27+
### Example - Update a Ticket
28+
```
29+
$Incident = Get-ServiceNowIncident -Limit 1 -MatchContains @{short_description='PowerShell'}
30+
Update-ServiceNowIncident -SysID $Incident.Sys_ID -Values @{comments='Updated via PowerShell'}
31+
```
32+
2733
### Azure Connection Object (Automation Integration Module Support)
2834
The module can use the `Connection` parameter in conjunction with the included `PSServiceNow-Automation.json` file for use as an Azure automation integration module. Details of the process is available at [Authoring Integration Modules for Azure Automation](https://azure.microsoft.com/en-us/blog/authoring-integration-modules-for-azure-automation).
2935

@@ -43,6 +49,9 @@ The `Connection` parameter accepts a hashtable object that requires a username,
4349
* Remove-ServiceNowTableEntry
4450
* Set-ServiceNowAuth
4551
* Test-ServiceNowAuthIsSet
52+
* Update-ServiceNowChangeRequest
53+
* Update-ServiceNowIncident
54+
* Update-ServiceNowTableEntry
4655

4756
## Tests
4857
This module comes with [Pester](https://github.com/pester/Pester/) tests for unit testing.

0 commit comments

Comments
 (0)