Skip to content

Commit 9a84e9a

Browse files
authored
Fix failure on successful record removal (#159)
* don't fail on 204 with delete * make get all tables optional * fix pipelining * release prep * changelog update
1 parent 8b05170 commit 9a84e9a

File tree

5 files changed

+77
-41
lines changed

5 files changed

+77
-41
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 3.1.1
2+
- Fix [#158](https://github.com/Snow-Shell/servicenow-powershell/issues/158), failure on successful record deletion
3+
- Move table details retrieval in `New-ServiceNowSession` to be switch operated, `-GetAllTable` and reduce function time
4+
- Fix pipelining in `Remove-ServiceNowRecord`
5+
16
## 3.1.0
27
- Add DateTime support to querying, [#68](https://github.com/Snow-Shell/servicenow-powershell/issues/68)
38
- Add `-between` operator

ServiceNow/Private/Invoke-ServiceNowRestMethod.ps1

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -156,22 +156,32 @@ function Invoke-ServiceNowRestMethod {
156156
throw $_
157157
}
158158

159-
# TODO: this could use some work
160-
# checking for content is good, but at times we'll get content that's not valid
161-
# eg. html content when a dev instance is hibernating
162-
if ( $response.Content ) {
163-
$content = $response.content | ConvertFrom-Json
164-
if ( $content.PSobject.Properties.Name -contains "result" ) {
165-
$records = @($content | Select-Object -ExpandProperty result)
159+
# validate response
160+
switch ($Method) {
161+
'Delete' {
162+
if ( $response.StatusCode -ne 204 ) {
163+
throw ('"{0} : {1}' -f $response.StatusCode, $response | Out-String )
164+
}
166165
}
167-
else {
168-
$records = @($content)
166+
Default {
167+
# TODO: this could use some work
168+
# checking for content is good, but at times we'll get content that's not valid
169+
# eg. html content when a dev instance is hibernating
170+
if ( $response.Content ) {
171+
$content = $response.content | ConvertFrom-Json
172+
if ( $content.PSobject.Properties.Name -contains "result" ) {
173+
$records = @($content | Select-Object -ExpandProperty result)
174+
}
175+
else {
176+
$records = @($content)
177+
}
178+
}
179+
else {
180+
# invoke-webrequest didn't throw an error per se, but we didn't get content back either
181+
throw ('"{0} : {1}' -f $response.StatusCode, $response | Out-String )
182+
}
169183
}
170184
}
171-
else {
172-
# invoke-webrequest didn't throw an error per se, but we didn't get content back either
173-
throw ('"{0} : {1}' -f $response.StatusCode, $response | Out-String )
174-
}
175185

176186
$totalRecordCount = 0
177187
if ( $response.Headers.'X-Total-Count' ) {
@@ -187,7 +197,10 @@ function Invoke-ServiceNowRestMethod {
187197
# if option to get all records was provided, loop and get them all
188198
if ( $PSCmdlet.PagingParameters.IncludeTotalCount.IsPresent ) {
189199

190-
Write-Warning "Getting $($totalRecordCount - $PSCmdlet.PagingParameters.Skip) records, this could take a while..."
200+
$retrieveRecordCount = $totalRecordCount - $PSCmdlet.PagingParameters.Skip
201+
if ( $retrieveRecordCount -ne 0 ) {
202+
Write-Warning "Getting $retrieveRecordCount records..."
203+
}
191204

192205
$setPoint = $params.body.sysparm_offset + $params.body.sysparm_limit
193206

ServiceNow/Public/New-ServiceNowSession.ps1

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ Credential of user who can access Proxy. If not provided, the current user will
2929
.PARAMETER ApiVersion
3030
Specific API version to use. The default is the latest.
3131
32+
.PARAMETER GetAllTable
33+
Populate $ServiceNowTable with data from all tables the user has access to
34+
3235
.PARAMETER PassThru
3336
Provide the resulting session object to the pipeline as opposed to setting as a script scoped variable to be used by default for other calls.
3437
This is useful if you want to have multiple sessions with different api versions, credentials, etc.
@@ -99,6 +102,9 @@ function New-ServiceNowSession {
99102
[Parameter()]
100103
[int] $ApiVersion,
101104

105+
[Parameter()]
106+
[switch] $GetAllTable,
107+
102108
[Parameter()]
103109
[switch] $PassThru
104110
)
@@ -217,25 +223,27 @@ function New-ServiceNowSession {
217223
$Script:ServiceNowSession = $newSession
218224
}
219225

220-
Write-Verbose 'Getting table number prefixes'
221-
$defaultTable = $ServiceNowTable
222-
try {
223-
$numbers = Get-ServiceNowRecord -Table 'sys_number' -Property prefix, category -First 10000
224-
foreach ($number in $numbers) {
225-
if ( $number.prefix.ToLower() -notin $defaultTable.NumberPrefix ) {
226-
$ServiceNowTable.Add(
227-
[pscustomobject] @{
228-
"Name" = ($number.category.link | Select-String -Pattern '^.*\?name=(.*)$').matches.groups[1].Value
229-
"ClassName" = $number.category.display_value
230-
"Type" = $null
231-
"NumberPrefix" = $number.prefix.ToLower()
232-
"DescriptionField" = "short_description"
233-
}
234-
) | Out-Null
226+
if ( $GetAllTable.IsPresent ) {
227+
Write-Verbose 'Getting table number prefixes'
228+
$defaultTable = $ServiceNowTable
229+
try {
230+
$numbers = Get-ServiceNowRecord -Table 'sys_number' -Property prefix, category -First 10000 -IncludeTotalCount
231+
foreach ($number in $numbers) {
232+
if ( $number.prefix.ToLower() -notin $defaultTable.NumberPrefix ) {
233+
$ServiceNowTable.Add(
234+
[pscustomobject] @{
235+
"Name" = ($number.category.link | Select-String -Pattern '^.*\?name=(.*)$').matches.groups[1].Value
236+
"ClassName" = $number.category.display_value
237+
"Type" = $null
238+
"NumberPrefix" = $number.prefix.ToLower()
239+
"DescriptionField" = "short_description"
240+
}
241+
) | Out-Null
242+
}
235243
}
236244
}
237-
}
238-
catch {
239-
Write-Verbose "Session created, but failed to populate ServiceNowTable. Prefixes beyond the default won't be available. $_"
245+
catch {
246+
Write-Verbose "Session created, but failed to populate ServiceNowTable. Prefixes beyond the default won't be available. $_"
247+
}
240248
}
241249
}
Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
function Remove-ServiceNowRecord {
2+
23
[CmdletBinding(ConfirmImpact = 'High')]
4+
35
Param(
46
# Table containing the entry we're deleting
57
[parameter(Mandatory, ValueFromPipelineByPropertyName)]
8+
[ValidateNotNullOrEmpty()]
69
[Alias('sys_class_name')]
710
[string] $Table,
811

912
# sys_id of the entry we're deleting
1013
[parameter(Mandatory, ValueFromPipelineByPropertyName)]
14+
[ValidateNotNullOrEmpty()]
1115
[Alias('sys_id')]
1216
[string] $SysId,
1317

@@ -18,13 +22,19 @@ function Remove-ServiceNowRecord {
1822
[hashtable] $ServiceNowSession = $script:ServiceNowSession
1923
)
2024

21-
$params = @{
22-
Method = 'Delete'
23-
Table = $Table
24-
SysId = $SysId
25-
Connection = $Connection
26-
ServiceNowSession = $ServiceNowSession
25+
begin {
26+
27+
}
28+
29+
process {
30+
$params = @{
31+
Method = 'Delete'
32+
Table = $Table
33+
SysId = $SysId
34+
Connection = $Connection
35+
ServiceNowSession = $ServiceNowSession
36+
}
37+
38+
Invoke-ServiceNowRestMethod @params
2739
}
28-
29-
Invoke-ServiceNowRestMethod @params
3040
}

ServiceNow/ServiceNow.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
RootModule = 'ServiceNow.psm1'
66

77
# Version number of this module.
8-
ModuleVersion = '3.1.0'
8+
ModuleVersion = '3.1.1'
99

1010
# ID used to uniquely identify this module
1111
GUID = 'b90d67da-f8d0-4406-ad74-89d169cd0633'

0 commit comments

Comments
 (0)