Skip to content

Commit b0fe4cd

Browse files
authored
Merge pull request #8 from Gilmond/master
Added incident and change update methods and tests for incident
2 parents 10945dc + e621e18 commit b0fe4cd

File tree

4 files changed

+205
-5
lines changed

4 files changed

+205
-5
lines changed

PSServiceNow-Changes.psm1

Lines changed: 51 additions & 0 deletions
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

Lines changed: 51 additions & 1 deletion
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

Lines changed: 77 additions & 0 deletions
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

Lines changed: 26 additions & 4 deletions
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
}

0 commit comments

Comments
 (0)