Skip to content

Commit b6bd572

Browse files
committed
table name fix
1 parent e238197 commit b6bd572

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

ServiceNow/Public/Get-ServiceNowRecord.ps1

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
.EXAMPLE
6969
Get-ServiceNowRecord -Table 'Catalog Task' -ParentId 'RITM0010001'
7070
Get tasks for the parent requested item
71-
71+
7272
.EXAMPLE
7373
Get-ServiceNowRecord -Table incident -Filter @('state', '-eq', '1') -Description 'powershell'
7474
Get incident records where state equals New or short description contains the word powershell
@@ -106,7 +106,7 @@
106106
.EXAMPLE
107107
gsnr RITM0010001
108108
Get a specific record by number using the function alias
109-
109+
110110
.INPUTS
111111
None
112112
@@ -205,13 +205,13 @@ function Get-ServiceNowRecord {
205205
}
206206
}
207207
}
208-
208+
209209
if ( $Id ) {
210210
if ( $Id -match '^[a-zA-Z0-9]{32}$' ) {
211211
if ( -not $thisTable ) {
212212
throw 'Providing sys_id for -Id requires a value for -Table. Alternatively, provide an Id with a prefix, eg. INC1234567, and the table will be automatically determined.'
213213
}
214-
214+
215215
$idFilter = @('sys_id', '-eq', $Id)
216216
}
217217
else {
@@ -226,19 +226,19 @@ function Get-ServiceNowRecord {
226226
}
227227
$idFilter = @('number', '-eq', $Id)
228228
}
229-
229+
230230
if ( $invokeParams.Filter ) {
231231
$invokeParams.Filter = $invokeParams.Filter, 'and', $idFilter
232232
}
233233
else {
234234
$invokeParams.Filter = $idFilter
235235
}
236-
236+
237237
}
238-
238+
239239
# we have the table, update the params
240240
$invokeParams.Table = $thisTable.Name
241-
241+
242242
if ( $ParentId ) {
243243
if ( $ParentId -match '^[a-zA-Z0-9]{32}$' ) {
244244
$parentIdFilter = @('parent.sys_id', '-eq', $ParentId)
@@ -254,11 +254,11 @@ function Get-ServiceNowRecord {
254254
$invokeParams.Filter = $parentIdFilter
255255
}
256256
}
257-
257+
258258
if ( $Description ) {
259259
# determine the field we should compare for 'description' and add the filter
260260
if ( -not $thisTable.DescriptionField ) {
261-
Write-Warning ('We do not have table ''{0}'' in the config; short_description will be used as the description field' -f $Table)
261+
Write-Warning ('We do not have table ''{0}'' in the config; short_description will be used as the description field' -f $thisTable.Name)
262262
$thisTable.DescriptionField = 'short_description'
263263
}
264264

@@ -281,11 +281,12 @@ function Get-ServiceNowRecord {
281281
}
282282

283283
# should use Get-ServiceNowAttachment, but put this here for ease of access
284-
if ( $Table -eq 'attachment' ) {
284+
if ( $thisTable.Name -eq 'attachment' ) {
285+
Write-Warning 'For attachments, use Get-ServiceNowAttachment'
285286
$invokeParams.Remove('Table') | Out-Null
286287
$invokeParams.UriLeaf = '/attachment'
287288
}
288-
289+
289290
$result = Invoke-ServiceNowRestMethod @invokeParams
290291

291292
if ( $result ) {
@@ -302,7 +303,7 @@ function Get-ServiceNowRecord {
302303

303304
if ( $customVars ) {
304305
$customValueParams = @{
305-
Table = $Table
306+
Table = $thisTable.Name
306307
Filter = @('sys_id', '-eq', $record.sys_id)
307308
Property = $customVars.'sc_item_option.item_option_new.name' | ForEach-Object { "variables.$_" }
308309
}

ServiceNow/Public/New-ServiceNowSession.ps1

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ function New-ServiceNowSession {
153153

154154
$oldProgressPreference = $ProgressPreference
155155
$ProgressPreference = 'SilentlyContinue'
156-
156+
157157
$response = Invoke-WebRequest @params
158158

159159
# set the progress pref back now that done with invoke-webrequest
@@ -217,4 +217,26 @@ function New-ServiceNowSession {
217217
else {
218218
$Script:ServiceNowSession = $newSession
219219
}
220+
221+
Write-Verbose 'Getting table number prefixes'
222+
$defaultTable = $ServiceNowTable
223+
try {
224+
$numbers = Get-ServiceNowRecord -Table 'sys_number' -Property prefix, category -First 10000
225+
foreach ($number in $numbers) {
226+
if ( $number.prefix.ToLower() -notin $defaultTable.NumberPrefix ) {
227+
$ServiceNowTable.Add(
228+
[pscustomobject] @{
229+
"Name" = ($number.category.link | Select-String -Pattern '^.*\?name=(.*)$').matches.groups[1].Value
230+
"ClassName" = $number.category.display_value
231+
"Type" = $null
232+
"NumberPrefix" = $number.prefix.ToLower()
233+
"DescriptionField" = "short_description"
234+
}
235+
) | Out-Null
236+
}
237+
}
238+
}
239+
catch {
240+
Write-Verbose "Session created, but failed to populate ServiceNowTable. Prefixes beyond the default won't be available. $_"
241+
}
220242
}

0 commit comments

Comments
 (0)