From fe9b6b879093feedbd1a360f8258573924abfa9f Mon Sep 17 00:00:00 2001 From: CATgwalker <78571293+CATgwalker@users.noreply.github.com> Date: Wed, 10 Apr 2024 15:53:15 -0500 Subject: [PATCH 1/3] Update Get-ServiceNowRecord.ps1 Fixes error "Id must either be a SysId 32 character alphanumeric or Number with prefix and id." and retains ID from reference table for further lookup. --- ServiceNow/Public/Get-ServiceNowRecord.ps1 | 33 ++++++++++------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/ServiceNow/Public/Get-ServiceNowRecord.ps1 b/ServiceNow/Public/Get-ServiceNowRecord.ps1 index 5441853..7e65143 100644 --- a/ServiceNow/Public/Get-ServiceNowRecord.ps1 +++ b/ServiceNow/Public/Get-ServiceNowRecord.ps1 @@ -173,8 +173,7 @@ function Get-ServiceNowRecord { [ValidateScript( { if ($_ -match '^[a-zA-Z0-9]{32}$' -or $_ -match '^([a-zA-Z]+)[0-9]+$') { $true - } - else { + } else { throw 'Id must either be a SysId 32 character alphanumeric or Number with prefix and id.' } })] @@ -185,8 +184,7 @@ function Get-ServiceNowRecord { [ValidateScript( { if ($_ -match '^[a-zA-Z0-9]{32}$' -or $_ -match '^([a-zA-Z]+)[0-9]+$') { $true - } - else { + } else { throw 'ParentId must either be a SysId 32 character alphanumeric or Number with prefix and id.' } })] @@ -269,8 +267,7 @@ function Get-ServiceNowRecord { if ( $thisID -match '^[a-zA-Z0-9]{32}$' ) { $thisParams.Filter += , @('sys_id', '-eq', $thisID) - } - else { + } else { $thisParams.Filter += , @('number', '-eq', $thisID) } } @@ -283,8 +280,7 @@ function Get-ServiceNowRecord { if ( $ParentID -match '^[a-zA-Z0-9]{32}$' ) { $thisParams.Filter += , @('parent.sys_id', '-eq', $ParentID) - } - else { + } else { $thisParams.Filter += , @('parent.number', '-eq', $ParentID) } @@ -363,7 +359,13 @@ function Get-ServiceNowRecord { # show the underlying value if the option is a reference type if ( $newVar.Type -eq 'Reference' ) { + #do not do any further lookup when the value is blank or null + #resolves #234 and 262 + if ($var.'sc_item_option.value' -eq "" -or $null -eq $var.'sc_item_option.value') { + continue + } $newVar | Add-Member @{'ReferenceTable' = $var.'sc_item_option.item_option_new.reference' } + $newVar | Add-Member @{'ReferenceID' = $var.'sc_item_option.value' } # issue 234. ID might not be sysid or number for reference...odd $refValue = Get-ServiceNowRecord -Table $var.'sc_item_option.item_option_new.reference' -ID $var.'sc_item_option.value' -Property name -AsValue -ServiceNowSession $ServiceNowSession -ErrorAction SilentlyContinue if ( $refValue ) { @@ -373,33 +375,28 @@ function Get-ServiceNowRecord { if ( $var.'sc_item_option.item_option_new.name' ) { $record.CustomVariable | Add-Member @{ $var.'sc_item_option.item_option_new.name' = $newVar } - } - else { + } else { $record.CustomVariable | Add-Member @{ $var.'sc_item_option.item_option_new.question_text' = $newVar } } } if ( $addedSysIdProp ) { $record | Select-Object -Property * -ExcludeProperty sys_id - } - else { + } else { $record } } - } - else { + } else { # format the results if ( $Property ) { if ( $Property.Count -eq 1 -and $AsValue ) { $propName = $result | Get-Member | Where-Object { $_.MemberType -eq 'NoteProperty' } | Select-Object -ExpandProperty Name $result.$propName - } - else { + } else { $result } - } - else { + } else { if ($thisTable.Type) { $result | ForEach-Object { $_.PSObject.TypeNames.Insert(0, $thisTable.Type) } } From ba19c85b8309f6f5c5a4e0261b13e128d423b2a8 Mon Sep 17 00:00:00 2001 From: CATgwalker <78571293+CATgwalker@users.noreply.github.com> Date: Tue, 4 Jun 2024 23:20:02 -0500 Subject: [PATCH 2/3] Update Get-ServiceNowRecord.ps1 Additional fixes for reference table lookup where value is not in the 32 character format expected by SNOW. --- ServiceNow/Public/Get-ServiceNowRecord.ps1 | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/ServiceNow/Public/Get-ServiceNowRecord.ps1 b/ServiceNow/Public/Get-ServiceNowRecord.ps1 index 7e65143..9966d26 100644 --- a/ServiceNow/Public/Get-ServiceNowRecord.ps1 +++ b/ServiceNow/Public/Get-ServiceNowRecord.ps1 @@ -364,10 +364,15 @@ function Get-ServiceNowRecord { if ($var.'sc_item_option.value' -eq "" -or $null -eq $var.'sc_item_option.value') { continue } - $newVar | Add-Member @{'ReferenceTable' = $var.'sc_item_option.item_option_new.reference' } - $newVar | Add-Member @{'ReferenceID' = $var.'sc_item_option.value' } - # issue 234. ID might not be sysid or number for reference...odd - $refValue = Get-ServiceNowRecord -Table $var.'sc_item_option.item_option_new.reference' -ID $var.'sc_item_option.value' -Property name -AsValue -ServiceNowSession $ServiceNowSession -ErrorAction SilentlyContinue + $sysidPattern = "[0-9a-fA-F]{32}" + $sysid = [Regex]::Matches($var.'sc_item_option.value', $sysidPattern).Value + if ($sysid) { + Write-Verbose "Custom variable lookup for $($newvar.name) from table '$($var.'sc_item_option.item_option_new.reference')' sysid:'$($var.'sc_item_option.value')'" + $newVar | Add-Member @{'ReferenceTable' = $var.'sc_item_option.item_option_new.reference' } + $newVar | Add-Member @{'ReferenceID' = $var.'sc_item_option.value' } + # issue 234. ID might not be sysid or number for reference...odd + $refValue = Get-ServiceNowRecord -Table $var.'sc_item_option.item_option_new.reference' -ID $var.'sc_item_option.value' -Property name -AsValue -ServiceNowSession $ServiceNowSession -ErrorAction SilentlyContinue + } if ( $refValue ) { $newVar.Value = $refValue } From 8869f17bb60ce31a884982d8f5e40bc8c06ad244 Mon Sep 17 00:00:00 2001 From: CATgwalker <78571293+CATgwalker@users.noreply.github.com> Date: Fri, 12 Jul 2024 08:42:58 -0500 Subject: [PATCH 3/3] Update ServiceNow/Public/Get-ServiceNowRecord.ps1 Co-authored-by: Greg Brownstein --- ServiceNow/Public/Get-ServiceNowRecord.ps1 | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ServiceNow/Public/Get-ServiceNowRecord.ps1 b/ServiceNow/Public/Get-ServiceNowRecord.ps1 index 9966d26..df04349 100644 --- a/ServiceNow/Public/Get-ServiceNowRecord.ps1 +++ b/ServiceNow/Public/Get-ServiceNowRecord.ps1 @@ -372,10 +372,11 @@ function Get-ServiceNowRecord { $newVar | Add-Member @{'ReferenceID' = $var.'sc_item_option.value' } # issue 234. ID might not be sysid or number for reference...odd $refValue = Get-ServiceNowRecord -Table $var.'sc_item_option.item_option_new.reference' -ID $var.'sc_item_option.value' -Property name -AsValue -ServiceNowSession $ServiceNowSession -ErrorAction SilentlyContinue - } - if ( $refValue ) { - $newVar.Value = $refValue - } + if ( $refValue ) { + $newVar.Value = $refValue + } + } + } if ( $var.'sc_item_option.item_option_new.name' ) {