Skip to content

Commit 3d8f02f

Browse files
committed
compare entire id prefix to avoid issues
1 parent 2968fa3 commit 3d8f02f

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

ServiceNow/Public/Get-ServiceNowRecord.ps1

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ function Get-ServiceNowRecord {
203203
}
204204

205205
if ( $Id ) {
206-
if ( $Id -match '[a-zA-Z0-9]{32}' ) {
206+
if ( $Id -match '^[a-zA-Z0-9]{32}$' ) {
207207
if ( -not $thisTable ) {
208208
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.'
209209
}
@@ -213,7 +213,9 @@ function Get-ServiceNowRecord {
213213
else {
214214
if ( -not $thisTable ) {
215215
# get table name from prefix if only Id was provided
216-
$thisTable = $script:ServiceNowTable | Where-Object { $_.NumberPrefix -and $Id.ToLower().StartsWith($_.NumberPrefix) }
216+
$idPrefix = ($Id | Select-String -Pattern '^([a-zA-Z]+)([0-9]+$)').Matches.Groups[1].Value.ToLower()
217+
Write-Debug "Id prefix is $idPrefix"
218+
$thisTable = $script:ServiceNowTable | Where-Object { $_.NumberPrefix -and $idPrefix -eq $_.NumberPrefix }
217219
if ( -not $thisTable ) {
218220
throw ('The prefix for Id ''{0}'' was not found and the appropriate table cannot be determined. Known prefixes are {1}. Please provide a value for -Table.' -f $Id, ($ServiceNowTable.NumberPrefix.Where( { $_ }) -join ', '))
219221
}
@@ -234,7 +236,7 @@ function Get-ServiceNowRecord {
234236
$invokeParams.Table = $thisTable.Name
235237

236238
if ( $ParentId ) {
237-
if ( $ParentId -match '[a-zA-Z0-9]{32}' ) {
239+
if ( $ParentId -match '^[a-zA-Z0-9]{32}$' ) {
238240
$parentIdFilter = @('parent.sys_id', '-eq', $ParentId)
239241
}
240242
else {

0 commit comments

Comments
 (0)