Skip to content

Commit 04173af

Browse files
authored
Merge pull request #61 from replicaJunction/master
Create function Get-ServiceNowRequestItem
2 parents 180cfa7 + d0f05b2 commit 04173af

File tree

4 files changed

+104
-1
lines changed

4 files changed

+104
-1
lines changed

Readme.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ These changes should improve your ability to filter on the right, especially by
3232

3333
Requires PowerShell 3.0 or above as this is when `Invoke-RestMethod` was introduced.
3434

35+
Requires authorization in your ServiceNow tenant. Due to the custom nature of ServiceNow your organization may have REST access restricted. The following are some tips to ask for if you're having to go to your admin for access:
36+
37+
* Out of the box tables should be accessible by granting the `ITIL` role.
38+
* Custom tables may require adjustments to the ACL.
39+
* The `Web_Service_Admin` role may also be an option.
40+
3541
## Usage
3642

3743
Download the [latest release](https://github.com/Sam-Martin/servicenow-powershell/releases/latest) and extract the .psm1 and .psd1 files to your PowerShell profile directory (i.e. the `Modules` directory under wherever `$profile` points to in your PS console) and run:
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
function Get-ServiceNowRequestItem {
2+
param(
3+
# Machine name of the field to order by
4+
[parameter(mandatory = $false)]
5+
[parameter(ParameterSetName = 'SpecifyConnectionFields')]
6+
[parameter(ParameterSetName = 'UseConnectionObject')]
7+
[parameter(ParameterSetName = 'SetGlobalAuth')]
8+
[string]$OrderBy = 'opened_at',
9+
10+
# Direction of ordering (Desc/Asc)
11+
[parameter(mandatory = $false)]
12+
[parameter(ParameterSetName = 'SpecifyConnectionFields')]
13+
[parameter(ParameterSetName = 'UseConnectionObject')]
14+
[parameter(ParameterSetName = 'SetGlobalAuth')]
15+
[ValidateSet("Desc", "Asc")]
16+
[string]$OrderDirection = 'Desc',
17+
18+
# Maximum number of records to return
19+
[parameter(mandatory = $false)]
20+
[parameter(ParameterSetName = 'SpecifyConnectionFields')]
21+
[parameter(ParameterSetName = 'UseConnectionObject')]
22+
[parameter(ParameterSetName = 'SetGlobalAuth')]
23+
[int]$Limit = 10,
24+
25+
# Hashtable containing machine field names and values returned must match exactly (will be combined with AND)
26+
[parameter(mandatory = $false)]
27+
[parameter(ParameterSetName = 'SpecifyConnectionFields')]
28+
[parameter(ParameterSetName = 'UseConnectionObject')]
29+
[parameter(ParameterSetName = 'SetGlobalAuth')]
30+
[hashtable]$MatchExact = @{},
31+
32+
# Hashtable containing machine field names and values returned rows must contain (will be combined with AND)
33+
[parameter(mandatory = $false)]
34+
[parameter(ParameterSetName = 'SpecifyConnectionFields')]
35+
[parameter(ParameterSetName = 'UseConnectionObject')]
36+
[parameter(ParameterSetName = 'SetGlobalAuth')]
37+
[hashtable]$MatchContains = @{},
38+
39+
# Whether or not to show human readable display values instead of machine values
40+
[parameter(mandatory = $false)]
41+
[parameter(ParameterSetName = 'SpecifyConnectionFields')]
42+
[parameter(ParameterSetName = 'UseConnectionObject')]
43+
[parameter(ParameterSetName = 'SetGlobalAuth')]
44+
[ValidateSet("true", "false", "all")]
45+
[string]$DisplayValues = 'true',
46+
47+
[Parameter(ParameterSetName = 'SpecifyConnectionFields', Mandatory = $True)]
48+
[ValidateNotNullOrEmpty()]
49+
[PSCredential]
50+
$ServiceNowCredential,
51+
52+
[Parameter(ParameterSetName = 'SpecifyConnectionFields', Mandatory = $True)]
53+
[ValidateNotNullOrEmpty()]
54+
[string]
55+
$ServiceNowURL,
56+
57+
[Parameter(ParameterSetName = 'UseConnectionObject', Mandatory = $True)]
58+
[ValidateNotNullOrEmpty()]
59+
[Hashtable]
60+
$Connection
61+
)
62+
63+
# Query Splat
64+
$newServiceNowQuerySplat = @{
65+
OrderBy = $OrderBy
66+
MatchExact = $MatchExact
67+
OrderDirection = $OrderDirection
68+
MatchContains = $MatchContains
69+
}
70+
$Query = New-ServiceNowQuery @newServiceNowQuerySplat
71+
72+
# Table Splat
73+
$getServiceNowTableSplat = @{
74+
Table = 'sc_req_item'
75+
Query = $Query
76+
Limit = $Limit
77+
DisplayValues = $DisplayValues
78+
}
79+
80+
# Update the Table Splat if the parameters have values
81+
if ($null -ne $PSBoundParameters.Connection) {
82+
$getServiceNowTableSplat.Add('Connection', $Connection)
83+
}
84+
elseif ($null -ne $PSBoundParameters.ServiceNowCredential -and $null -ne $PSBoundParameters.ServiceNowURL) {
85+
$getServiceNowTableSplat.Add('ServiceNowCredential', $ServiceNowCredential)
86+
$getServiceNowTableSplat.Add('ServiceNowURL', $ServiceNowURL)
87+
}
88+
89+
# Perform query and return each object in the format.ps1xml format
90+
$Result = Get-ServiceNowTable @getServiceNowTableSplat
91+
$Result | ForEach-Object {$_.PSObject.TypeNames.Insert(0, "ServiceNow.RequestItem")}
92+
$Result
93+
}

ServiceNow/ServiceNow.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ FormatsToProcess = @('ServiceNow.format.ps1xml')
6666
NestedModules = @()
6767

6868
# Functions to export from this module
69-
FunctionsToExport = @('Get-ServiceNowChangeRequest','Get-ServiceNowConfigurationItem','Get-ServiceNowIncident','Get-ServiceNowRequest','Get-ServiceNowTable','Get-ServiceNowTableEntry','Get-ServiceNowUser','Get-ServiceNowUserGroup','New-ServiceNowIncident','New-ServiceNowQuery','New-ServiceNowTableEntry','Remove-ServiceNowAuth','Remove-ServiceNowTableEntry','Set-ServiceNowAuth','Test-ServiceNowAuthIsSet','Update-ServiceNowChangeRequest','Update-ServiceNowIncident','Update-ServiceNowNumber','Update-ServiceNowTableEntry')
69+
FunctionsToExport = @('Get-ServiceNowChangeRequest','Get-ServiceNowConfigurationItem','Get-ServiceNowIncident','Get-ServiceNowRequest','Get-ServiceNowRequestItem','Get-ServiceNowTable','Get-ServiceNowTableEntry','Get-ServiceNowUser','Get-ServiceNowUserGroup','New-ServiceNowIncident','New-ServiceNowQuery','New-ServiceNowTableEntry','Remove-ServiceNowAuth','Remove-ServiceNowTableEntry','Set-ServiceNowAuth','Test-ServiceNowAuthIsSet','Update-ServiceNowChangeRequest','Update-ServiceNowIncident','Update-ServiceNowNumber','Update-ServiceNowTableEntry')
7070

7171
# List of all modules packaged with this module
7272
# ModuleList = @()

Tests/ServiceNow.Tests.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ Describe "ServiceNow-Module" {
7979
([array](Get-ServiceNowRequest)).count -gt 0 | Should -Match $true
8080
}
8181

82+
It "Get-ServiceNowRequestItem returns records" {
83+
([array](Get-ServiceNowRequestItem)).count -gt 0 | Should -Match $true
84+
}
85+
8286
It "Get-ServiceNowUserGroup works" {
8387
(Get-ServiceNowUserGroup).Count -gt 0 | Should -Match $true
8488
}

0 commit comments

Comments
 (0)