Skip to content

Commit b3554db

Browse files
committed
1.8.0 - Formatting on Update-ServiceNowRequestItem & Added Pester Test
1 parent c5578e7 commit b3554db

File tree

3 files changed

+100
-26
lines changed

3 files changed

+100
-26
lines changed
Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,82 @@
11
function Update-ServiceNowRequestItem {
2-
Param
3-
( # sys_id of the caller of the Request Item (use Get-ServiceNowUser to retrieve this)
4-
[parameter(mandatory=$true)]
5-
[parameter(ParameterSetName='SpecifyConnectionFields', mandatory=$true)]
6-
[parameter(ParameterSetName='UseConnectionObject', mandatory=$true)]
7-
[parameter(ParameterSetName='SetGlobalAuth', mandatory=$true)]
2+
<#
3+
.SYNOPSIS
4+
Update an existing request item (RITM)
5+
6+
.DESCRIPTION
7+
Update an existing request item (RITM)
8+
9+
.EXAMPLE
10+
Update-ServiceNowRequestItem -SysId $SysId -Values @{property='value'}
11+
12+
Updates a ticket number with a value providing no return output.
13+
14+
.EXAMPLE
15+
Update-ServiceNowRequestItem -SysId $SysId -Values @{property='value'} -PassThru
16+
17+
Updates a ticket number with a value providing return output.
18+
19+
.NOTES
20+
21+
#>
22+
23+
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingConvertToSecureStringWithPlainText','')]
24+
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidGlobalVars','')]
25+
26+
[OutputType([void],[System.Management.Automation.PSCustomObject])]
27+
[CmdletBinding(DefaultParameterSetName,SupportsShouldProcess=$true)]
28+
Param (
29+
# sys_id of the ticket to update
30+
[Parameter(mandatory=$true)]
831
[string]$SysId,
932

10-
# Hashtable of values to use as the record's properties
11-
[parameter(mandatory=$true)]
33+
# Hashtable of values to use as the record's properties
34+
[Parameter(mandatory=$true)]
1235
[hashtable]$Values,
1336

14-
# Credential used to authenticate to ServiceNow
37+
# Credential used to authenticate to ServiceNow
1538
[Parameter(ParameterSetName='SpecifyConnectionFields', Mandatory=$True)]
1639
[ValidateNotNullOrEmpty()]
17-
[PSCredential]$ServiceNowCredential,
40+
[Alias('ServiceNowCredential')]
41+
[PSCredential]$Credential,
1842

1943
# The URL for the ServiceNow instance being used (eg: instancename.service-now.com)
2044
[Parameter(ParameterSetName='SpecifyConnectionFields', Mandatory=$True)]
2145
[ValidateNotNullOrEmpty()]
22-
[string]$ServiceNowURL,
46+
[string]$ServiceNowURL,
2347

2448
#Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
25-
[Parameter(ParameterSetName='UseConnectionObject', Mandatory=$True)]
49+
[Parameter(ParameterSetName='UseConnectionObject', Mandatory=$True)]
2650
[ValidateNotNullOrEmpty()]
27-
[Hashtable]$Connection
28-
)
51+
[Hashtable]$Connection,
52+
53+
# Switch to allow the results to be passed back
54+
[Parameter(Mandatory=$false)]
55+
[switch]$PassThru
56+
)
2957

3058
$updateServiceNowTableEntrySplat = @{
31-
SysId = $SysId
32-
Table = 'sc_req_item'
59+
SysId = $SysId
60+
Table = 'sc_req_item'
3361
Values = $Values
3462
}
35-
63+
3664
# Update the splat if the parameters have values
37-
if ($null -ne $PSBoundParameters.Connection)
38-
{
65+
If ($null -ne $PSBoundParameters.Connection) {
3966
$updateServiceNowTableEntrySplat.Add('Connection',$Connection)
4067
}
41-
elseif ($null -ne $PSBoundParameters.ServiceNowCredential -and $null -ne $PSBoundParameters.ServiceNowURL)
42-
{
68+
ElseIf ($null -ne $PSBoundParameters.Credential -and $null -ne $PSBoundParameters.ServiceNowURL) {
4369
$updateServiceNowTableEntrySplat.Add('ServiceNowCredential',$ServiceNowCredential)
4470
$updateServiceNowTableEntrySplat.Add('ServiceNowURL',$ServiceNowURL)
4571
}
46-
47-
Update-ServiceNowTableEntry @updateServiceNowTableEntrySplat
48-
}
4972

73+
If ($PSCmdlet.ShouldProcess("$Table/$SysID",$MyInvocation.MyCommand)) {
74+
# Send REST call
75+
$Result = Update-ServiceNowTableEntry @updateServiceNowTableEntrySplat
76+
77+
# Option to return results
78+
If ($PSBoundParameters.ContainsKey('Passthru')) {
79+
$Result
80+
}
81+
}
82+
}

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.7.1'
15+
ModuleVersion = '1.8.0'
1616

1717
# ID used to uniquely identify this module
1818
GUID = 'b90d67da-f8d0-4406-ad74-89d169cd0633'
@@ -130,6 +130,8 @@ PrivateData = @{
130130

131131

132132

133+
134+
133135

134136

135137

Tests/ServiceNow.Tests.ps1

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,45 @@ Describe "ServiceNow-Module" {
253253
$TestTicket.description | Should -Be 'Updated by Pester test Update-ServiceNowNumber with SpecifyConnectionFields works'
254254
}
255255

256+
It "Update-ServiceNowRequestItem No PassThru works" {
257+
# Due to a lack of ServiceNow request (REQ) commands this test only works consistently in a developer instance
258+
$TestTicket = Get-ServiceNowRequestItem -MatchExact @{number='RITM0000001';short_description='Apple iPad 3';state=1} -ErrorAction SilentlyContinue
259+
$TestTicket.number | Should -Be 'RITM0000001' -Because 'This test only works in a ServiceNow developer instance for RITM0000001'
260+
261+
$Values = @{
262+
'description' = 'Updated by Pester test Update-ServiceNowRequestItem No PassThru works'
263+
}
264+
265+
$CommandOutput = Update-ServiceNowRequestItem -SysId $TestTicket.sys_id -Values $Values
266+
267+
$TestTicket = Get-ServiceNowRequestItem -MatchExact @{sys_id=$TestTicket.sys_id}
268+
$TestTicket.description | Should -Be 'Updated by Pester test Update-ServiceNowRequestItem No PassThru works'
269+
$CommandOutput | Should -BeNullOrEmpty
270+
}
271+
272+
It "Update-ServiceNowRequestItem with SpecifyConnectionFields and PassThru works" {
273+
# Due to a lack of ServiceNow request (REQ) commands this test only works consistently in a developer instance
274+
$TestTicket = Get-ServiceNowRequestItem -MatchExact @{number='RITM0000001';short_description='Apple iPad 3';state=1} -ErrorAction SilentlyContinue
275+
$TestTicket.number | Should -Be 'RITM0000001' -Because 'This test only works in a ServiceNow developer instance for RITM0000001'
276+
277+
$Values = @{
278+
'description' = 'Updated by Pester test Update-ServiceNowRequestItem with SpecifyConnectionFields works'
279+
}
280+
281+
$updateServiceNowRequestItemSplat = @{
282+
SysID = $TestTicket.sys_id
283+
Values = $Values
284+
Credential = $Credential
285+
ServiceNowURL = $Defaults.ServiceNowURL
286+
PassThru = $true
287+
}
288+
$CommandOutput = Update-ServiceNowRequestItem @updateServiceNowRequestItemSplat
289+
290+
$TestTicket = Get-ServiceNowRequestItem -MatchExact @{sys_id=$TestTicket.sys_id}
291+
$TestTicket.description | Should -Be 'Updated by Pester test Update-ServiceNowRequestItem with SpecifyConnectionFields works'
292+
$CommandOutput | Should -Not -BeNullOrEmpty
293+
}
294+
256295
# Remove Functions
257296
It "Remove-ServiceNowTable works" {
258297
$TestTicket = Get-ServiceNowIncident -First 1
@@ -270,6 +309,6 @@ Describe "ServiceNow-Module" {
270309
}
271310

272311
It "Remove-ServiceNowAuth works" {
273-
Remove-ServiceNowAuth | Should be $true
312+
Remove-ServiceNowAuth | Should -Be $true
274313
}
275314
}

0 commit comments

Comments
 (0)