From 91229418eb28f80ce7961f8522aa2ef0473fd024 Mon Sep 17 00:00:00 2001 From: marvint24 Date: Tue, 30 Sep 2025 07:43:12 +0200 Subject: [PATCH 1/3] implement dot walking --- ServiceNow/Public/Get-ServiceNowRecord.ps1 | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/ServiceNow/Public/Get-ServiceNowRecord.ps1 b/ServiceNow/Public/Get-ServiceNowRecord.ps1 index 62b7a54..6461dc7 100644 --- a/ServiceNow/Public/Get-ServiceNowRecord.ps1 +++ b/ServiceNow/Public/Get-ServiceNowRecord.ps1 @@ -59,6 +59,11 @@ Only valid when the Property parameter is set to 1 item. Helpful when retrieving sys_id for example. +.PARAMETER EnableDotWalking + Returns an pscustomobject that supports dot-walking for reference fields. + This option will automatically add getter methods for refrence fields. + When you access the property, a call will be made to ServiceNow to retrieve the referenced record. + .PARAMETER Connection Azure Automation Connection object containing username, password, and URL for the ServiceNow instance @@ -141,6 +146,12 @@ Get-ServiceNowRecord -Table 'cmdb_ci' -Property sys_id -First 1 -AsValue Get the underlying value for a property instead of a pscustomobject where the value needs to be extracted + +.EXAMPLE + $incident=Get-ServiceNowRecord -Table 'incident' -First 1 -EnableDotWalking + $incident.caller_id.manager.email + + Get the email address of the manager of the caller for an incident using dot-walking .EXAMPLE gsnr RITM0010001 @@ -231,6 +242,9 @@ function Get-ServiceNowRecord { [Parameter()] [switch] $AsValue, + + [Parameter()] + [switch] $EnableDotWalking, [Parameter()] [hashtable] $Connection, @@ -341,6 +355,34 @@ function Get-ServiceNowRecord { if ( -not $Property -and $result[0].PSObject.Properties.name -notcontains 'sys_class_name' ) { $result | Add-Member @{'sys_class_name' = $Table } } + + if ($EnableDotWalking) { + foreach ($record in $result) { + $newObj = New-Object PSCustomObject + foreach ($key in $record.PSObject.Properties.Name) { + $value = $record.$key + # if the value is a reference + if ($value -and ($value.GetType().Name -eq "PSCustomObject") -and ($value.link -ne $null)) { + $refTable = $value.link.split("/")[6] + $refId = $value.link.split("/")[7] + + # there are some fields that seem to be references, but are different + if ($refId -notmatch '^[a-zA-Z0-9]{32}$') { + $newObj | Add-Member -MemberType NoteProperty -Name $key -Value $value + continue + } + + $newObj | Add-Member -MemberType ScriptProperty -Name $key -Value { + Get-ServiceNowRecord -Table $refTable -ID $refId -ServiceNowSession $ServiceNowSession + }.GetNewClosure() + } + else { + $newObj | Add-Member -MemberType NoteProperty -Name $key -Value $value + } + } + } + $result = $newObj + } if ( $IncludeCustomVariable ) { From 540d329b7d38a82177dd758798fc0a1e3fc7aa7f Mon Sep 17 00:00:00 2001 From: marvint24 Date: Tue, 30 Sep 2025 07:46:50 +0200 Subject: [PATCH 2/3] format --- ServiceNow/Public/Get-ServiceNowRecord.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ServiceNow/Public/Get-ServiceNowRecord.ps1 b/ServiceNow/Public/Get-ServiceNowRecord.ps1 index 6461dc7..9d86e61 100644 --- a/ServiceNow/Public/Get-ServiceNowRecord.ps1 +++ b/ServiceNow/Public/Get-ServiceNowRecord.ps1 @@ -33,7 +33,7 @@ For a complete list of comparison operators, see $script:ServiceNowOperator and use Name in your filter. See the examples. - .PARAMETER FilterString +.PARAMETER FilterString A string representation of the filter. This is useful when the filter is complex and hard to specify as an array. Retrieve the filter string from the ServiceNow UI via right click on the filter and selecting 'Copy query'. @@ -120,7 +120,7 @@ Get-ServiceNowRecord -Table incident -Filter @('state', '-eq', '1') -Sort @('opened_at', 'desc'), @('state') Get incident records where state equals New and first sort by the field opened_at descending and then sort by the field state ascending -] + .EXAMPLE Get-ServiceNowRecord -Table 'change request' -Filter @('opened_at', '-ge', (Get-Date).AddDays(-30)) @@ -158,7 +158,7 @@ Get a specific record by number using the function alias - .EXAMPLE +.EXAMPLE Get-ServiceNowRecord -Table 'incident' -FilterString 'active=true^state=1' Provide a filter string from the UI to get records where active is true and state is 1 From b8d18be4f02995633445e9d3569dc005c3f41991 Mon Sep 17 00:00:00 2001 From: marvint24 Date: Tue, 30 Sep 2025 08:00:20 +0200 Subject: [PATCH 3/3] add recurse --- ServiceNow/Public/Get-ServiceNowRecord.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ServiceNow/Public/Get-ServiceNowRecord.ps1 b/ServiceNow/Public/Get-ServiceNowRecord.ps1 index 9d86e61..3cf2051 100644 --- a/ServiceNow/Public/Get-ServiceNowRecord.ps1 +++ b/ServiceNow/Public/Get-ServiceNowRecord.ps1 @@ -373,7 +373,7 @@ function Get-ServiceNowRecord { } $newObj | Add-Member -MemberType ScriptProperty -Name $key -Value { - Get-ServiceNowRecord -Table $refTable -ID $refId -ServiceNowSession $ServiceNowSession + Get-ServiceNowRecord -Table $refTable -ID $refId -ServiceNowSession $ServiceNowSession -EnableDotWalking }.GetNewClosure() } else {