|
| 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, by either table name or class name |
| 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 'change request' -Filter @('opened_at', '-ge', 'javascript:gs.daysAgoEnd(30)') |
| 59 | + Get change requests opened in the last 30 days. Use class name as opposed to table name. |
| 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 |
| 69 | +#> |
| 70 | +function Get-ServiceNowRecord { |
| 71 | + |
| 72 | + [OutputType([System.Management.Automation.PSCustomObject])] |
| 73 | + [CmdletBinding(DefaultParameterSetName = 'SessionFilter', SupportsPaging)] |
| 74 | + |
| 75 | + Param ( |
| 76 | + [parameter(Mandatory)] |
| 77 | + [ValidateNotNullOrEmpty()] |
| 78 | + [Alias('sys_class_name')] |
| 79 | + [string] $Table, |
| 80 | + |
| 81 | + [Parameter()] |
| 82 | + [Alias('Fields')] |
| 83 | + [string[]] $Properties, |
| 84 | + |
| 85 | + [parameter(ParameterSetName = 'AutomationFilter')] |
| 86 | + [parameter(ParameterSetName = 'SessionFilter')] |
| 87 | + [System.Collections.ArrayList] $Filter, |
| 88 | + |
| 89 | + [parameter(ParameterSetName = 'AutomationFilter')] |
| 90 | + [parameter(ParameterSetName = 'SessionFilter')] |
| 91 | + [ValidateNotNullOrEmpty()] |
| 92 | + [System.Collections.ArrayList] $Sort, |
| 93 | + |
| 94 | + [Parameter()] |
| 95 | + [ValidateSet('true', 'false', 'all')] |
| 96 | + [string] $DisplayValues = 'true', |
| 97 | + |
| 98 | + [Parameter(Mandatory, ParameterSetName = 'AutomationQuery')] |
| 99 | + [parameter(Mandatory, ParameterSetName = 'AutomationFilter')] |
| 100 | + [ValidateNotNullOrEmpty()] |
| 101 | + [hashtable] $Connection, |
| 102 | + |
| 103 | + [Parameter(ParameterSetName = 'SessionQuery')] |
| 104 | + [Parameter(ParameterSetName = 'SessionFilter')] |
| 105 | + [ValidateNotNullOrEmpty()] |
| 106 | + [hashtable] $ServiceNowSession = $script:ServiceNowSession |
| 107 | + ) |
| 108 | + |
| 109 | + $result = Invoke-ServiceNowRestMethod @PSBoundParameters |
| 110 | + |
| 111 | + If ( $result -and -not $Properties) { |
| 112 | + $type = $script:ServiceNowTable | Where-Object {$_.Name -eq $Table -or $_.ClassName -eq $Table} | Select-Object -ExpandProperty Type |
| 113 | + if ($type) { |
| 114 | + $result | ForEach-Object { $_.PSObject.TypeNames.Insert(0, $type) } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + $result |
| 119 | +} |
0 commit comments