Skip to content

Commit 8b5d875

Browse files
committed
help updates
1 parent b8f603e commit 8b5d875

File tree

3 files changed

+92
-50
lines changed

3 files changed

+92
-50
lines changed

ServiceNow/Public/Get-ServiceNowRecord.ps1

Lines changed: 75 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,86 @@
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
}

ServiceNow/Public/New-ServiceNowQuery.ps1

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
1616
.PARAMETER Filter
1717
Array or multidimensional array of fields and values to filter on.
18-
Each array should be of the format @(field, comparison operator, value) separated by a join, either 'and', 'or', or 'group.
18+
Each array should be of the format @(field, comparison operator, value) separated by a join, either 'and', 'or', or 'group'.
1919
For a complete list of comparison operators, see $script:ServiceNowOperator and use Name in your filter.
2020
See the examples.
2121
Also, see https://docs.servicenow.com/bundle/quebec-platform-user-interface/page/use/common-ui-elements/reference/r_OpAvailableFiltersQueries.html
@@ -101,7 +101,7 @@ function New-ServiceNowQuery {
101101
if ( $Filter ) {
102102
$filterList = $Filter
103103
# see if we're working with 1 array or multidimensional array
104-
# we're looking for multidimensional so convert if not
104+
# we want multidimensional so convert if not
105105
if ($Filter[0].GetType().Name -eq 'String') {
106106
$filterList = @(, $Filter)
107107
}
@@ -149,7 +149,7 @@ function New-ServiceNowQuery {
149149
}
150150

151151
2 {
152-
# should be a non-value operator
152+
# should be a non-value operator, eg. ='' / ISEMPTY
153153
$thisOperator = $script:ServiceNowOperator | Where-Object { $_.Name -eq $thisFilter[1] }
154154
if ( -not $thisOperator ) {
155155
throw ('Operator ''{0}'' is not valid' -f $thisFilter[1])
@@ -161,7 +161,7 @@ function New-ServiceNowQuery {
161161
}
162162

163163
3 {
164-
# should be key operator value
164+
# should be field operator value
165165
$thisOperator = $script:ServiceNowOperator | Where-Object { $_.Name -eq $thisFilter[1] }
166166
if ( -not $thisOperator ) {
167167
throw ('Operator ''{0}'' is not valid', $thisFilter[1])
@@ -186,7 +186,7 @@ function New-ServiceNowQuery {
186186

187187
$orderList = $Sort
188188
# see if we're working with 1 array or multidimensional array
189-
# we're looking for multidimensional so convert if not
189+
# we want multidimensional so convert if not
190190
if ($Sort[0].GetType().Name -eq 'String') {
191191
$orderList = @(, $Sort)
192192
}

ServiceNow/Public/New-ServiceNowSession.ps1

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -166,17 +166,18 @@ function New-ServiceNowSession {
166166
}
167167
}
168168

169-
$cmdbParams = @{
170-
Table = 'sys_db_object'
171-
Query = 'nameSTARTSWITHcmdb_ci'
172-
Properties = 'name', 'sys_id', 'label'
173-
First = 10000
174-
ServiceNowSession = $newSession
175-
}
176-
$ci = Get-ServiceNowTable @cmdbParams -ErrorAction SilentlyContinue
177-
if ( $ci ) {
178-
$newSession.Add('CmdbClasses', $ci)
179-
}
169+
# TODO
170+
# $cmdbParams = @{
171+
# Table = 'sys_db_object'
172+
# Query = 'nameSTARTSWITHcmdb_ci'
173+
# Properties = 'name', 'sys_id', 'label'
174+
# First = 10000
175+
# ServiceNowSession = $newSession
176+
# }
177+
# $ci = Get-ServiceNowTable @cmdbParams -ErrorAction SilentlyContinue
178+
# if ( $ci ) {
179+
# $newSession.Add('CmdbClasses', $ci)
180+
# }
180181

181182
Write-Verbose ($newSession | ConvertTo-Json)
182183

0 commit comments

Comments
 (0)