1- function Get-ServiceNowRecord {
2- <#
3- . SYNOPSIS
4- Retrieves records for the specified table
5- . DESCRIPTION
6- The Get-ServiceNowTable function retrieves records for the specified table
7- . INPUTS
8- None
9- . OUTPUTS
10- System.Management.Automation.PSCustomObject
11- . LINK
12- Service-Now Kingston REST Table API: https://docs.servicenow.com/bundle/kingston-application-development/page/integrate/inbound-rest/concept/c_TableAPI.html
13- Service-Now Table API FAQ: https://hi.service-now.com/kb_view.do?sysparm_article=KB0534905
1+ <#
2+ . SYNOPSIS
3+ Retrieves records for the specified table
4+
5+ . DESCRIPTION
6+ Retrieve records from any table with the option to filter, sort, and choose fields.
7+ Given you know the table name, you shouldn't need any other 'Get-' function.
8+
9+ . PARAMETER Table
10+ Name of the table to be queried
11+
12+ . PARAMETER Properties
13+ Limit the fields returned to this list
14+
15+ . PARAMETER Filter
16+ Array or multidimensional array of fields and values to filter on.
17+ Each array should be of the format @(field, comparison operator, value) separated by a join, either 'and', 'or', or 'group'.
18+ For a complete list of comparison operators, see $script:ServiceNowOperator and use Name in your filter.
19+ See the examples.
20+ Also, see https://docs.servicenow.com/bundle/quebec-platform-user-interface/page/use/common-ui-elements/reference/r_OpAvailableFiltersQueries.html
21+ for how to represent date values with javascript.
22+
23+ . PARAMETER Sort
24+ Array or multidimensional array of fields to sort on.
25+ Each array should be of the format @(field, asc/desc).
26+
27+ . PARAMETER DisplayValues
28+ Option to display values for reference fields.
29+ 'false' will only retrieve the reference
30+ 'true' will only retrieve the underlying value
31+ 'all' will retrieve both. This is helpful when trying to translate values for a query.
32+
33+ . PARAMETER Connection
34+ Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
35+
36+ . PARAMETER ServiceNowSession
37+ ServiceNow session created by New-ServiceNowSession. Will default to script-level variable $ServiceNowSession.
38+
39+ . EXAMPLE
40+ Get-ServiceNowRecord -Table incident -Filter @('state', '-eq', '1'), 'or', @('short_description','-like', 'powershell')
41+ Get incident records where state equals New or short description contains the word powershell
42+
43+ . EXAMPLE
44+ $filter = @('state', '-eq', '1'),
45+ 'and',
46+ @('short_description','-like', 'powershell'),
47+ 'group',
48+ @('state', '-eq', '2')
49+ PS > Get-ServiceNowRecord -Table incident -Filter $filter
50+ Get incident records where state equals New and short description contains the word powershell or state equals In Progress.
51+ The first 2 filters are combined and then or'd against the last.
52+
53+ . EXAMPLE
54+ Get-ServiceNowRecord -Table incident -Filter @('state', '-eq', '1') -Sort @('opened_at', 'desc'), @('state')
55+ Get incident records where state equals New and first sort by the field opened_at descending and then sort by the field state ascending
56+
57+ . EXAMPLE
58+ Get-ServiceNowRecord -Table incident -Filter @('opened_at', '-ge', 'javascript:gs.daysAgoEnd(30)')
59+ Get incident records opened in the last 30 days
60+
61+ . INPUTS
62+ None
63+
64+ . OUTPUTS
65+ System.Management.Automation.PSCustomObject
66+
67+ . LINK
68+ https://docs.servicenow.com/bundle/quebec-platform-user-interface/page/use/common-ui-elements/reference/r_OpAvailableFiltersQueries.html
1469#>
70+ function Get-ServiceNowRecord {
1571
1672 [OutputType ([System.Management.Automation.PSCustomObject ])]
17- [CmdletBinding (DefaultParameterSetName = ' Session ' , SupportsPaging )]
73+ [CmdletBinding (DefaultParameterSetName = ' SessionFilter ' , SupportsPaging )]
1874
1975 Param (
20- # Name of the table we're querying (e.g. incidents)
2176 [parameter (Mandatory )]
2277 [ValidateNotNullOrEmpty ()]
2378 [string ] $Table ,
2479
80+ [Parameter ()]
81+ [Alias (' Fields' )]
82+ [string []] $Properties ,
83+
2584 [parameter (ParameterSetName = ' AutomationFilter' )]
2685 [parameter (ParameterSetName = ' SessionFilter' )]
2786 [System.Collections.ArrayList ] $Filter ,
@@ -31,17 +90,6 @@ function Get-ServiceNowRecord {
3190 [ValidateNotNullOrEmpty ()]
3291 [System.Collections.ArrayList ] $Sort ,
3392
34- # sysparm_query param in the format of a ServiceNow encoded query string (see http://wiki.servicenow.com/index.php?title=Encoded_Query_Strings)
35- [Parameter (Mandatory , ParameterSetName = ' AutomationQuery' )]
36- [Parameter (Mandatory , ParameterSetName = ' SessionQuery' )]
37- [string ] $Query ,
38-
39- # Fields to return
40- [Parameter ()]
41- [Alias (' Fields' )]
42- [string []] $Properties ,
43-
44- # Whether or not to show human readable display values instead of machine values
4593 [Parameter ()]
4694 [ValidateSet (' true' , ' false' , ' all' )]
4795 [string ] $DisplayValues = ' true' ,
@@ -57,17 +105,10 @@ function Get-ServiceNowRecord {
57105 [hashtable ] $ServiceNowSession = $script :ServiceNowSession
58106 )
59107
60- # $params = $PSBoundParameters
61-
62- # Add all provided paging parameters
63- # ($PSCmdlet.PagingParameters | Get-Member -MemberType Property).Name | ForEach-Object {
64- # $params.Add($_, $PSCmdlet.PagingParameters.$_)
65- # }
66-
67108 $result = Invoke-ServiceNowRestMethod @PSBoundParameters
68109
69110 If ( $result -and -not $Properties ) {
70- $type = $script :ServiceNowTable | Where-Object {$_.DbTableName -eq $Table } | Select-Object - ExpandProperty Type
111+ $type = $script :ServiceNowTable | Where-Object {$_.Name -eq $Table } | Select-Object - ExpandProperty Type
71112 if ($type ) {
72113 $result | ForEach-Object { $_.PSObject.TypeNames.Insert (0 , $type ) }
73114 }
0 commit comments