From f5d2d5d17aed901a60e4a0122e542432356c5d59 Mon Sep 17 00:00:00 2001 From: Greg Brownstein Date: Thu, 11 Jul 2024 16:13:30 -0400 Subject: [PATCH] support filter string from ui --- .../Private/Invoke-ServiceNowRestMethod.ps1 | 12 +++++++++-- ServiceNow/Public/Get-ServiceNowRecord.ps1 | 20 ++++++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/ServiceNow/Private/Invoke-ServiceNowRestMethod.ps1 b/ServiceNow/Private/Invoke-ServiceNowRestMethod.ps1 index 1cd6244..36f3716 100644 --- a/ServiceNow/Private/Invoke-ServiceNowRestMethod.ps1 +++ b/ServiceNow/Private/Invoke-ServiceNowRestMethod.ps1 @@ -45,6 +45,9 @@ function Invoke-ServiceNowRestMethod { [parameter()] [object[]] $Filter, + [parameter()] + [string] $FilterString, + [parameter()] [object[]] $Sort = @('opened_at', 'desc'), @@ -98,10 +101,15 @@ function Invoke-ServiceNowRestMethod { if ( $Method -eq 'Get') { $Body = @{ 'sysparm_display_value' = $DisplayValue - 'sysparm_query' = (New-ServiceNowQuery -Filter $Filter -Sort $Sort) 'sysparm_limit' = 10 } + if ( $FilterString ) { + $Body.sysparm_query = $FilterString + } else { + $Body.sysparm_query = (New-ServiceNowQuery -Filter $Filter -Sort $Sort) + } + # Handle paging parameters # The value of -First defaults to [uint64]::MaxValue if not specified. # If no paging information was provided, default to the legacy behavior, which was to return 10 records. @@ -275,4 +283,4 @@ function Invoke-ServiceNowRestMethod { } $records -} +} \ No newline at end of file diff --git a/ServiceNow/Public/Get-ServiceNowRecord.ps1 b/ServiceNow/Public/Get-ServiceNowRecord.ps1 index 5441853..8259f4d 100644 --- a/ServiceNow/Public/Get-ServiceNowRecord.ps1 +++ b/ServiceNow/Public/Get-ServiceNowRecord.ps1 @@ -33,6 +33,10 @@ For a complete list of comparison operators, see $script:ServiceNowOperator and use Name in your filter. See the examples. + .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'. + .PARAMETER Sort Array or multidimensional array of fields to sort on. Each array should be of the format @(field, asc/desc). @@ -143,6 +147,11 @@ Get a specific record by number using the function alias + .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 + .INPUTS ID @@ -165,6 +174,7 @@ function Get-ServiceNowRecord { [Parameter(ParameterSetName = 'Table', Mandatory)] [Parameter(ParameterSetName = 'TableId', Mandatory)] [Parameter(ParameterSetName = 'TableParentId')] + [Parameter(ParameterSetName = 'FilterString', Mandatory)] [Alias('sys_class_name')] [string] $Table, @@ -204,6 +214,10 @@ function Get-ServiceNowRecord { [Parameter(ParameterSetName = 'TableParentId')] [object[]] $Filter = @(), + [Parameter(ParameterSetName = 'FilterString', Mandatory)] + [Alias('fs')] + [string] $FilterString, + [Parameter(ParameterSetName = 'Table')] [Parameter(ParameterSetName = 'TableParentId')] [ValidateNotNullOrEmpty()] @@ -234,7 +248,6 @@ function Get-ServiceNowRecord { process { $thisParams = @{ - Filter = $Filter Property = $Property Sort = $Sort DisplayValue = $DisplayValue @@ -246,6 +259,7 @@ function Get-ServiceNowRecord { } if ( $PSBoundParameters.ContainsKey('Filter') ) { + $thisParams.Filter = $Filter # # we always want the filter to be arrays separated by joins if ( $Filter[0].GetType().Name -ne 'Object[]' ) { # @@ -253,6 +267,10 @@ function Get-ServiceNowRecord { } } + if ( $FilterString ) { + $thisParams.FilterString = $FilterString + } + $addedSysIdProp = $false # we need the sys_id value in order to get custom var data # add it in if specific properties were requested and not part of the list