Skip to content

Commit 61686e2

Browse files
authored
Merge pull request #72 from Sam-Martin/development
v1.5.0 - SupportsPaging Feature Addition
2 parents 162d8cc + 0e55e6b commit 61686e2

11 files changed

+143
-29
lines changed

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# ServiceNow
22

3-
[![GitHub release](https://img.shields.io/github/release/Sam-Martin/servicenow-powershell.svg)](https://github.com/Sam-Martin/servicenow-powershell/releases/latest) [![GitHub license](https://img.shields.io/github/license/Sam-Martin/servicenow-powershell.svg)](LICENSE) ![Test Coverage](https://img.shields.io/badge/coverage-78%25-yellow.svg)
3+
[![GitHub release](https://img.shields.io/github/release/Sam-Martin/servicenow-powershell.svg)](https://github.com/Sam-Martin/servicenow-powershell/releases/latest) [![GitHub license](https://img.shields.io/github/license/Sam-Martin/servicenow-powershell.svg)](LICENSE) ![Test Coverage](https://img.shields.io/badge/coverage-79%25-yellow.svg)
44

55
This PowerShell module provides a series of cmdlets for interacting with the [ServiceNow REST API](http://wiki.servicenow.com/index.php?title=REST_API), performed by wrapping `Invoke-RestMethod` for the API calls.
66

ServiceNow/Public/Get-ServiceNowChangeRequest.ps1

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function Get-ServiceNowChangeRequest {
22
[OutputType([System.Management.Automation.PSCustomObject])]
3-
[CmdletBinding(DefaultParameterSetName)]
3+
[CmdletBinding(DefaultParameterSetName, SupportsPaging)]
44
Param(
55
# Machine name of the field to order by
66
[Parameter(Mandatory = $false)]
@@ -13,7 +13,7 @@ function Get-ServiceNowChangeRequest {
1313

1414
# Maximum number of records to return
1515
[Parameter(Mandatory = $false)]
16-
[int]$Limit = 10,
16+
[int]$Limit,
1717

1818
# Fields to return
1919
[Parameter(Mandatory = $false)]
@@ -61,7 +61,6 @@ function Get-ServiceNowChangeRequest {
6161
$getServiceNowTableSplat = @{
6262
Table = 'change_request'
6363
Query = $Query
64-
Limit = $Limit
6564
Fields = $Properties
6665
DisplayValues = $DisplayValues
6766
}
@@ -75,6 +74,16 @@ function Get-ServiceNowChangeRequest {
7574
$getServiceNowTableSplat.Add('ServiceNowURL',$ServiceNowURL)
7675
}
7776

77+
# Only add the Limit parameter if it was explicitly provided
78+
if ($PSBoundParameters.ContainsKey('Limit')) {
79+
$getServiceNowTableSplat.Add('Limit', $Limit)
80+
}
81+
82+
# Add all provided paging parameters
83+
($PSCmdlet.PagingParameters | Get-Member -MemberType Property).Name | Foreach-Object {
84+
$getServiceNowTableSplat.Add($_, $PSCmdlet.PagingParameters.$_)
85+
}
86+
7887
# Perform query and return each object in the format.ps1xml format
7988
$Result = Get-ServiceNowTable @getServiceNowTableSplat
8089
If (-not $Properties) {

ServiceNow/Public/Get-ServiceNowConfigurationItem.ps1

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function Get-ServiceNowConfigurationItem {
22
[OutputType([System.Management.Automation.PSCustomObject])]
3-
[CmdletBinding(DefaultParameterSetName)]
3+
[CmdletBinding(DefaultParameterSetName, SupportsPaging)]
44
Param(
55
# Machine name of the field to order by
66
[Parameter(Mandatory = $false)]
@@ -13,7 +13,7 @@ function Get-ServiceNowConfigurationItem {
1313

1414
# Maximum number of records to return
1515
[Parameter(Mandatory = $false)]
16-
[int]$Limit = 10,
16+
[int]$Limit,
1717

1818
# Fields to return
1919
[Parameter(Mandatory = $false)]
@@ -61,7 +61,6 @@ function Get-ServiceNowConfigurationItem {
6161
$getServiceNowTableSplat = @{
6262
Table = 'cmdb_ci'
6363
Query = $Query
64-
Limit = $Limit
6564
Fields = $Properties
6665
DisplayValues = $DisplayValues
6766
}
@@ -75,6 +74,16 @@ function Get-ServiceNowConfigurationItem {
7574
$getServiceNowTableSplat.Add('ServiceNowURL',$ServiceNowURL)
7675
}
7776

77+
# Only add the Limit parameter if it was explicitly provided
78+
if ($PSBoundParameters.ContainsKey('Limit')) {
79+
$getServiceNowTableSplat.Add('Limit', $Limit)
80+
}
81+
82+
# Add all provided paging parameters
83+
($PSCmdlet.PagingParameters | Get-Member -MemberType Property).Name | Foreach-Object {
84+
$getServiceNowTableSplat.Add($_, $PSCmdlet.PagingParameters.$_)
85+
}
86+
7887
# Perform query and return each object in the format.ps1xml format
7988
$Result = Get-ServiceNowTable @getServiceNowTableSplat
8089
If (-not $Properties) {

ServiceNow/Public/Get-ServiceNowIncident.ps1

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function Get-ServiceNowIncident{
22
[OutputType([System.Management.Automation.PSCustomObject])]
3-
[CmdletBinding(DefaultParameterSetName)]
3+
[CmdletBinding(DefaultParameterSetName, SupportsPaging)]
44
Param(
55
# Machine name of the field to order by
66
[Parameter(Mandatory = $false)]
@@ -13,7 +13,7 @@ function Get-ServiceNowIncident{
1313

1414
# Maximum number of records to return
1515
[Parameter(Mandatory = $false)]
16-
[int]$Limit = 10,
16+
[int]$Limit,
1717

1818
# Fields to return
1919
[Parameter(Mandatory = $false)]
@@ -61,7 +61,6 @@ function Get-ServiceNowIncident{
6161
$getServiceNowTableSplat = @{
6262
Table = 'incident'
6363
Query = $Query
64-
Limit = $Limit
6564
Fields = $Properties
6665
DisplayValues = $DisplayValues
6766
}
@@ -75,6 +74,16 @@ function Get-ServiceNowIncident{
7574
$getServiceNowTableSplat.Add('ServiceNowURL',$ServiceNowURL)
7675
}
7776

77+
# Only add the Limit parameter if it was explicitly provided
78+
if ($PSBoundParameters.ContainsKey('Limit')) {
79+
$getServiceNowTableSplat.Add('Limit', $Limit)
80+
}
81+
82+
# Add all provided paging parameters
83+
($PSCmdlet.PagingParameters | Get-Member -MemberType Property).Name | Foreach-Object {
84+
$getServiceNowTableSplat.Add($_, $PSCmdlet.PagingParameters.$_)
85+
}
86+
7887
# Perform query and return each object in the format.ps1xml format
7988
$Result = Get-ServiceNowTable @getServiceNowTableSplat
8089
If (-not $Properties) {

ServiceNow/Public/Get-ServiceNowRequest.ps1

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function Get-ServiceNowRequest {
22
[OutputType([System.Management.Automation.PSCustomObject])]
3-
[CmdletBinding(DefaultParameterSetName)]
3+
[CmdletBinding(DefaultParameterSetName, SupportsPaging)]
44
Param(
55
# Machine name of the field to order by
66
[Parameter(Mandatory = $false)]
@@ -13,7 +13,7 @@ function Get-ServiceNowRequest {
1313

1414
# Maximum number of records to return
1515
[Parameter(Mandatory = $false)]
16-
[int]$Limit = 10,
16+
[int]$Limit,
1717

1818
# Fields to return
1919
[Parameter(Mandatory = $false)]
@@ -61,7 +61,6 @@ function Get-ServiceNowRequest {
6161
$getServiceNowTableSplat = @{
6262
Table = 'sc_request'
6363
Query = $Query
64-
Limit = $Limit
6564
Fields = $Properties
6665
DisplayValues = $DisplayValues
6766
}
@@ -75,6 +74,16 @@ function Get-ServiceNowRequest {
7574
$getServiceNowTableSplat.Add('ServiceNowURL', $ServiceNowURL)
7675
}
7776

77+
# Only add the Limit parameter if it was explicitly provided
78+
if ($PSBoundParameters.ContainsKey('Limit')) {
79+
$getServiceNowTableSplat.Add('Limit', $Limit)
80+
}
81+
82+
# Add all provided paging parameters
83+
($PSCmdlet.PagingParameters | Get-Member -MemberType Property).Name | Foreach-Object {
84+
$getServiceNowTableSplat.Add($_, $PSCmdlet.PagingParameters.$_)
85+
}
86+
7887
# Perform query and return each object in the format.ps1xml format
7988
$Result = Get-ServiceNowTable @getServiceNowTableSplat
8089
If (-not $Properties) {

ServiceNow/Public/Get-ServiceNowRequestItem.ps1

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function Get-ServiceNowRequestItem {
1616
#>
1717

1818
[OutputType([System.Management.Automation.PSCustomObject])]
19-
[CmdletBinding(DefaultParameterSetName)]
19+
[CmdletBinding(DefaultParameterSetName, SupportsPaging)]
2020
param(
2121
# Machine name of the field to order by
2222
[parameter(Mandatory = $false)]
@@ -29,7 +29,7 @@ function Get-ServiceNowRequestItem {
2929

3030
# Maximum number of records to return
3131
[parameter(Mandatory = $false)]
32-
[int]$Limit = 10,
32+
[int]$Limit,
3333

3434
# Fields to return
3535
[Parameter(Mandatory = $false)]
@@ -76,7 +76,6 @@ function Get-ServiceNowRequestItem {
7676
$getServiceNowTableSplat = @{
7777
Table = 'sc_req_item'
7878
Query = $Query
79-
Limit = $Limit
8079
Fields = $Properties
8180
DisplayValues = $DisplayValues
8281
}
@@ -90,6 +89,16 @@ function Get-ServiceNowRequestItem {
9089
$getServiceNowTableSplat.Add('ServiceNowURL', $ServiceNowURL)
9190
}
9291

92+
# Only add the Limit parameter if it was explicitly provided
93+
if ($PSBoundParameters.ContainsKey('Limit')) {
94+
$getServiceNowTableSplat.Add('Limit', $Limit)
95+
}
96+
97+
# Add all provided paging parameters
98+
($PSCmdlet.PagingParameters | Get-Member -MemberType Property).Name | Foreach-Object {
99+
$getServiceNowTableSplat.Add($_, $PSCmdlet.PagingParameters.$_)
100+
}
101+
93102
# Perform query and return each object in the format.ps1xml format
94103
$Result = Get-ServiceNowTable @getServiceNowTableSplat
95104
If (-not $Properties) {

ServiceNow/Public/Get-ServiceNowTable.ps1

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function Get-ServiceNowTable {
1414
#>
1515

1616
[OutputType([System.Management.Automation.PSCustomObject])]
17-
[CmdletBinding(DefaultParameterSetName)]
17+
[CmdletBinding(DefaultParameterSetName, SupportsPaging)]
1818
Param (
1919
# Name of the table we're querying (e.g. incidents)
2020
[parameter(Mandatory = $true)]
@@ -27,7 +27,7 @@ function Get-ServiceNowTable {
2727

2828
# Maximum number of records to return
2929
[Parameter(Mandatory = $false)]
30-
[int]$Limit = 10,
30+
[int]$Limit,
3131

3232
# Fields to return
3333
[Parameter(Mandatory = $false)]
@@ -72,8 +72,48 @@ function Get-ServiceNowTable {
7272
throw "Exception: You must do one of the following to authenticate: `n 1. Call the Set-ServiceNowAuth cmdlet `n 2. Pass in an Azure Automation connection object `n 3. Pass in an endpoint and credential"
7373
}
7474

75+
$Body = @{'sysparm_display_value' = $DisplayValues}
76+
77+
# Handle paging parameters
78+
# If -Limit was provided, write a warning message, but prioritize it over -First.
79+
# The value of -First defaults to [uint64]::MaxValue if not specified.
80+
# If no paging information was provided, default to the legacy behavior, which was to return 10 records.
81+
82+
if ($PSBoundParameters.ContainsKey('Limit')) {
83+
Write-Warning "The -Limit parameter is deprecated, and may be removed in a future release. Use the -First parameter instead."
84+
$Body['sysparm_limit'] = $Limit
85+
}
86+
elseif ($PSCmdlet.PagingParameters.First -ne [uint64]::MaxValue) {
87+
$Body['sysparm_limit'] = $PSCmdlet.PagingParameters.First
88+
}
89+
else {
90+
$Body['sysparm_limit'] = 10
91+
}
92+
93+
if ($PSCmdlet.PagingParameters.Skip) {
94+
$Body['sysparm_offset'] = $PSCmdlet.PagingParameters.Skip
95+
}
96+
97+
if ($PSCmdlet.PagingParameters.IncludeTotalCount) {
98+
# Accuracy is a double between 0.0 and 1.0 representing an estimated percentage accuracy.
99+
# 0.0 means we have no idea and 1.0 means the number is exact.
100+
101+
# ServiceNow does return this information in the X-Total-Count response header,
102+
# but we're currently using Invoke-RestMethod to perform the API call, and Invoke-RestMethod
103+
# does not provide the response headers, so we can't capture this info.
104+
105+
# To properly support this parameter, we'd need to fall back on Invoke-WebRequest, read the
106+
# X-Total-Count header of the response, and update this parameter after performing the API
107+
# call.
108+
109+
# Reference:
110+
# https://developer.servicenow.com/app.do#!/rest_api_doc?v=jakarta&id=r_TableAPI-GET
111+
112+
[double] $accuracy = 0.0
113+
$PSCmdlet.PagingParameters.NewTotalCount($PSCmdlet.PagingParameters.First, $accuracy)
114+
}
115+
75116
# Populate the query
76-
$Body = @{'sysparm_limit' = $Limit; 'sysparm_display_value' = $DisplayValues}
77117
if ($Query) {
78118
$Body.sysparm_query = $Query
79119
}

ServiceNow/Public/Get-ServiceNowTableEntry.ps1

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function Get-ServiceNowTableEntry {
1919
2020
#>
2121

22-
[CmdletBinding(DefaultParameterSetName)]
22+
[CmdletBinding(DefaultParameterSetName, SupportsPaging)]
2323
param(
2424
# Table containing the entry we're deleting
2525
[parameter(mandatory = $true)]
@@ -36,7 +36,7 @@ function Get-ServiceNowTableEntry {
3636

3737
# Maximum number of records to return
3838
[parameter(Mandatory = $false)]
39-
[int]$Limit = 10,
39+
[int]$Limit,
4040

4141
# Fields to return
4242
[Parameter(Mandatory = $false)]
@@ -86,7 +86,6 @@ function Get-ServiceNowTableEntry {
8686
$getServiceNowTableSplat = @{
8787
Table = $Table
8888
Query = $Query
89-
Limit = $Limit
9089
Fields = $Properties
9190
DisplayValues = $DisplayValues
9291
ErrorAction = 'Stop'
@@ -104,6 +103,16 @@ function Get-ServiceNowTableEntry {
104103
Default {}
105104
}
106105

106+
# Only add the Limit parameter if it was explicitly provided
107+
if ($PSBoundParameters.ContainsKey('Limit')) {
108+
$getServiceNowTableSplat.Add('Limit', $Limit)
109+
}
110+
111+
# Add all provided paging parameters
112+
($PSCmdlet.PagingParameters | Get-Member -MemberType Property).Name | Foreach-Object {
113+
$getServiceNowTableSplat.Add($_, $PSCmdlet.PagingParameters.$_)
114+
}
115+
107116
# Perform table query and return each object. No fancy formatting here as this can pull tables with unknown default properties
108117
Get-ServiceNowTable @getServiceNowTableSplat
109118
}

ServiceNow/Public/Get-ServiceNowUser.ps1

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function Get-ServiceNowUser{
22
[OutputType([System.Management.Automation.PSCustomObject])]
3-
[CmdletBinding(DefaultParameterSetName)]
3+
[CmdletBinding(DefaultParameterSetName, SupportsPaging)]
44
Param(
55
# Machine name of the field to order by
66
[Parameter(Mandatory = $false)]
@@ -13,7 +13,7 @@ function Get-ServiceNowUser{
1313

1414
# Maximum number of records to return
1515
[Parameter(Mandatory = $false)]
16-
[int]$Limit = 10,
16+
[int]$Limit,
1717

1818
# Fields to return
1919
[Parameter(Mandatory = $false)]
@@ -61,7 +61,6 @@ function Get-ServiceNowUser{
6161
$getServiceNowTableSplat = @{
6262
Table = 'sys_user'
6363
Query = $Query
64-
Limit = $Limit
6564
Fields = $Properties
6665
DisplayValues = $DisplayValues
6766
}
@@ -75,6 +74,16 @@ function Get-ServiceNowUser{
7574
$getServiceNowTableSplat.Add('ServiceNowURL', $ServiceNowURL)
7675
}
7776

77+
# Only add the Limit parameter if it was explicitly provided
78+
if ($PSBoundParameters.ContainsKey('Limit')) {
79+
$getServiceNowTableSplat.Add('Limit', $Limit)
80+
}
81+
82+
# Add all provided paging parameters
83+
($PSCmdlet.PagingParameters | Get-Member -MemberType Property).Name | Foreach-Object {
84+
$getServiceNowTableSplat.Add($_, $PSCmdlet.PagingParameters.$_)
85+
}
86+
7887
# Perform query and return each object in the format.ps1xml format
7988
$Result = Get-ServiceNowTable @getServiceNowTableSplat
8089
If (-not $Properties) {

0 commit comments

Comments
 (0)