Skip to content

Commit ef7aef9

Browse files
committed
help and other minor updates
1 parent 20d6bfe commit ef7aef9

File tree

8 files changed

+96
-31
lines changed

8 files changed

+96
-31
lines changed

CHANGELOG.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
## v2.2
2-
- Add advanced filtering and sorting. Initially implemented with `New-ServiceNowQuery` and `Get-ServiceNowRecord`. Filter with many different comparison operators as well as 'and', 'or', and 'group'ing. Sort ascending or descending against multiple fields. Comparison operators are the same as PowerShell for ease of use.
2+
- Add advanced filtering and sorting. Initially implemented with `New-ServiceNowQuery` and `Get-ServiceNowRecord`. Filter with many different comparison operators as well as 'and', 'or', and 'group'ing. Sort ascending or descending against multiple fields. Comparison operators are the same as PowerShell for ease of use. Please use the GitHub Discussions section to provide feedback, thoughts, etc.
33
- Add `Get-ServiceNowRecord`. This function implements the new advanced filtering and sorting. As long as you know your table name, this can replace all other Get functions.
4-
- Enumerate implemented tables and advanced filtering operators in a json config to easily manage going forward; make available via script session variables.
5-
Be able to reference type names from this config per table, removing the need to have separate Get functions for every table.
4+
- Enumerate implemented tables and advanced filtering operators in a json config to easily manage going forward; make available via script scoped variables.
5+
Be able to reference types from this config per table, removing the need to have separate Get functions for every table.
6+
- Add type for catalog task
7+
- Fix error when getting an empty result from the api and performing a type lookup
8+
- Rename `Get-ServiceNowRequestItem` to `Get-ServiceNowRequestedItem` which is the actual name. Function alias created.
69

710
## v2.1
811
- Add proxy support to `New-ServiceNowSession`, [#97](https://github.com/Snow-Shell/servicenow-powershell/issues/97).

Readme.md

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Building on the great work the community has done thus far, a lot of new updates
1818

1919
## Requirements
2020

21-
Requires PowerShell 3.0 or above as this is when `Invoke-RestMethod` was introduced.
21+
Requires PowerShell 5.1 or above.
2222

2323
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:
2424

@@ -32,25 +32,52 @@ The ServiceNow module should be installed from the PowerShell Gallery with `inst
3232

3333
### Creating a new session
3434

35+
Creating a new session will create a script scoped variable `$ServiceNowSession` which will be used by default in other functions.
36+
37+
Basic authentication with just a credential...
3538
```PowerShell
36-
New-ServiceNowSession -url InstanceName.service-now.com -Credentials (Get-Credential)
39+
$params @{
40+
Url = 'instance.service-now.com'
41+
Credential = $userCred
42+
}
43+
New-ServiceNowSession @params
3744
```
3845

39-
This example is using basic authentication, but OAuth is available as well; see the built-in help for `New-ServiceNowSession`. All examples below assume a new session has already been created.
46+
Oauth authentication with user credential as well as application/client credential. The application/client credential can be found in the System OAuth->Application Registry section of ServiceNow.
47+
```PowerShell
48+
$params @{
49+
Url = 'instance.service-now.com'
50+
Credential = $userCred
51+
ClientCredential = $clientCred
52+
}
53+
New-ServiceNowSession @params
54+
```
4055

41-
### Example - Retrieving an Incident Containing the Word 'PowerShell'
56+
All examples below assume a new session has already been created.
57+
58+
### Getting incidents opened in the last 30 days
59+
```PowerShell
60+
$filter = @('opened_at', '-ge', 'javascript:gs.daysAgoEnd(30)')
61+
Get-ServiceNowRecord -Table incident -Filter $filter
62+
```
63+
64+
### Retrieving an Incident Containing the Word 'PowerShell'
4265

4366
```PowerShell
4467
Get-ServiceNowIncident -MatchContains @{short_description='PowerShell'}
4568
```
69+
or new with v2.2
70+
```PowerShell
71+
Get-ServiceNowRecord -Table incident -Filter @('short_description','-eq','PowerShell')
72+
```
4673

47-
### Example - Update a Ticket
74+
### Update a Ticket
4875

4976
```PowerShell
5077
Get-ServiceNowIncident -Limit 1 -MatchContains @{short_description='PowerShell'} | Update-ServiceNowIncident -Values @{comments='Updated via PowerShell'}
5178
```
5279

53-
### Example - Creating a Incident with custom table entries
80+
### Creating an Incident with custom table entries
5481

5582
```PowerShell
5683
$IncidentParams = @{Caller = "UserName"

ServiceNow/Private/Invoke-ServiceNowRestMethod.ps1

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ function Invoke-ServiceNowRestMethod {
2323
# Name of the table we're querying (e.g. incidents)
2424
[parameter(Mandatory, ParameterSetName = 'Table')]
2525
[ValidateNotNullOrEmpty()]
26+
[Alias('sys_class_name')]
2627
[string] $Table,
2728

2829
[parameter(ParameterSetName = 'Table')]
@@ -89,7 +90,15 @@ function Invoke-ServiceNowRestMethod {
8990
$params.ContentType = 'application/json'
9091

9192
if ( $Table ) {
92-
$params.Uri += "/table/$Table"
93+
# table can either be the actual table name or class name
94+
# look up the actual table name
95+
$tableName = $script:ServiceNowTable | Where-Object { $_.Name -eq $Table -or $_.ClassName -eq $Table } | Select-Object -ExpandProperty Name
96+
# if not in our lookup, just use the table name as provided
97+
if ( -not $tableName ) {
98+
$tableName = $Table
99+
}
100+
101+
$params.Uri += "/table/$tableName"
93102
if ( $SysId ) {
94103
$params.Uri += "/$SysId"
95104
}
@@ -168,7 +177,7 @@ function Invoke-ServiceNowRestMethod {
168177

169178
switch ($Method) {
170179
'Get' {
171-
if ( $response.result ) {
180+
if ( $response.PSobject.Properties.Name -contains "result" ) {
172181

173182
$result = $response | Select-Object -ExpandProperty result
174183
$ConvertToDateField = @('closed_at', 'expected_start', 'follow_up', 'opened_at', 'sys_created_on', 'sys_updated_on', 'work_end', 'work_start')

ServiceNow/Public/Get-ServiceNowRecord.ps1

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Given you know the table name, you shouldn't need any other 'Get-' function.
88
99
.PARAMETER Table
10-
Name of the table to be queried
10+
Name of the table to be queried, by either table name or class name
1111
1212
.PARAMETER Properties
1313
Limit the fields returned to this list
@@ -55,8 +55,8 @@
5555
Get incident records where state equals New and first sort by the field opened_at descending and then sort by the field state ascending
5656
5757
.EXAMPLE
58-
Get-ServiceNowRecord -Table incident -Filter @('opened_at', '-ge', 'javascript:gs.daysAgoEnd(30)')
59-
Get incident records opened in the last 30 days
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.
6060
6161
.INPUTS
6262
None
@@ -75,6 +75,7 @@ function Get-ServiceNowRecord {
7575
Param (
7676
[parameter(Mandatory)]
7777
[ValidateNotNullOrEmpty()]
78+
[Alias('sys_class_name')]
7879
[string] $Table,
7980

8081
[Parameter()]
@@ -108,7 +109,7 @@ function Get-ServiceNowRecord {
108109
$result = Invoke-ServiceNowRestMethod @PSBoundParameters
109110

110111
If ( $result -and -not $Properties) {
111-
$type = $script:ServiceNowTable | Where-Object {$_.Name -eq $Table} | Select-Object -ExpandProperty Type
112+
$type = $script:ServiceNowTable | Where-Object {$_.Name -eq $Table -or $_.ClassName -eq $Table} | Select-Object -ExpandProperty Type
112113
if ($type) {
113114
$result | ForEach-Object { $_.PSObject.TypeNames.Insert(0, $type) }
114115
}

ServiceNow/Public/New-ServiceNowSession.ps1

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

169-
# TODO
169+
# Write-Verbose 'Retrieving list of classes for this instance. This will take a few seconds...'
170170
# $cmdbParams = @{
171171
# Table = 'sys_db_object'
172-
# Query = 'nameSTARTSWITHcmdb_ci'
172+
# # Query = 'nameSTARTSWITHcmdb_ci'
173173
# Properties = 'name', 'sys_id', 'label'
174-
# First = 10000
174+
# First = 100000
175175
# ServiceNowSession = $newSession
176176
# }
177-
# $ci = Get-ServiceNowTable @cmdbParams -ErrorAction SilentlyContinue
178-
# if ( $ci ) {
179-
# $newSession.Add('CmdbClasses', $ci)
177+
178+
# $class = Get-ServiceNowTable @cmdbParams -ErrorAction SilentlyContinue |
179+
# Select-Object @{
180+
# 'n' = 'Name'
181+
# 'e' = { $_.name }
182+
# },
183+
# @{
184+
# 'n' = 'SysId'
185+
# 'e' = { $_.sys_id }
186+
# },
187+
# @{
188+
# 'n' = 'ClassName'
189+
# 'e' = { $_.label }
190+
# }
191+
# if ( $class ) {
192+
# $newSession.Add('Classes', $class)
180193
# }
181194

182195
Write-Verbose ($newSession | ConvertTo-Json)

ServiceNow/ServiceNow.psd1

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

6161
# Functions to export from this module
62-
FunctionsToExport = @('Get-ServiceNowRecord','New-ServiceNowSession','Add-ServiceNowAttachment','Get-ServiceNowAttachment','Get-ServiceNowAttachmentDetail','Get-ServiceNowChangeRequest','Get-ServiceNowConfigurationItem','Get-ServiceNowIncident','Get-ServiceNowRequest','Get-ServiceNowRequestItem','Get-ServiceNowTable','Get-ServiceNowTableEntry','Get-ServiceNowUser','Get-ServiceNowUserGroup','New-ServiceNowChangeRequest','New-ServiceNowIncident','New-ServiceNowQuery','New-ServiceNowTableEntry','Remove-ServiceNowAttachment','Remove-ServiceNowAuth','Remove-ServiceNowTableEntry','Set-ServiceNowAuth','Test-ServiceNowAuthIsSet','Update-ServiceNowChangeRequest','Update-ServiceNowIncident','Update-ServiceNowNumber','Update-ServiceNowRequestItem','Update-ServiceNowTableEntry')
62+
FunctionsToExport = @('Get-ServiceNowRecord','New-ServiceNowSession','Add-ServiceNowAttachment','Get-ServiceNowAttachment','Get-ServiceNowAttachmentDetail','Get-ServiceNowChangeRequest','Get-ServiceNowConfigurationItem','Get-ServiceNowIncident','Get-ServiceNowRequest','Get-ServiceNowRequestedItem','Get-ServiceNowTable','Get-ServiceNowTableEntry','Get-ServiceNowUser','Get-ServiceNowUserGroup','New-ServiceNowChangeRequest','New-ServiceNowIncident','New-ServiceNowQuery','New-ServiceNowTableEntry','Remove-ServiceNowAttachment','Remove-ServiceNowAuth','Remove-ServiceNowTableEntry','Set-ServiceNowAuth','Test-ServiceNowAuthIsSet','Update-ServiceNowChangeRequest','Update-ServiceNowIncident','Update-ServiceNowNumber','Update-ServiceNowRequestItem','Update-ServiceNowTableEntry')
6363

6464
# Variables to export from this module
6565
VariablesToExport = 'ServiceNowSession', 'ServiceNowOperator'

ServiceNow/ServiceNow.psm1

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#Requires -Version 3.0
21
[cmdletbinding()]
32
param()
43

@@ -26,4 +25,12 @@ foreach ($Folder in @('Private', 'Public')) {
2625
Export-ModuleMember -Function (Get-ChildItem -Path "$PSScriptRoot\Public\*.ps1").BaseName
2726

2827
$Script:ServiceNowSession = @{}
29-
Export-ModuleMember -Variable ServiceNowSession
28+
Export-ModuleMember -Variable ServiceNowSession
29+
30+
$aliases = @{
31+
'Get-ServiceNowRequestItem' = 'Get-ServiceNowRequestedItem'
32+
}
33+
$aliases.GetEnumerator() | ForEach-Object {
34+
Set-Alias -Name $_.Key -Value $_.Value
35+
}
36+
Export-ModuleMember -Alias *

ServiceNow/config/main.json

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,43 @@
22
"Tables": [
33
{
44
"Name": "incident",
5-
"CommonName": "Incident",
5+
"ClassName": "Incident",
66
"Type": "ServiceNow.Incident"
77
},
88
{
99
"Name": "change_request",
10-
"CommonName": "Change Request",
10+
"ClassName": "Change Request",
1111
"Type": "ServiceNow.ChangeRequest"
1212
},
1313
{
1414
"Name": "cmdb_ci",
15-
"CommonName": "Configuration Item",
15+
"ClassName": "Configuration Item",
1616
"Type": "ServiceNow.ConfigurationItem"
1717
},
1818
{
1919
"Name": "sc_request",
20-
"CommonName": "Request",
20+
"ClassName": "Request",
2121
"Type": "ServiceNow.Request"
2222
},
2323
{
2424
"Name": "sc_req_item",
25-
"CommonName": "Request Item",
25+
"ClassName": "Request Item",
2626
"Type": "ServiceNow.RequestItem"
2727
},
2828
{
2929
"Name": "sys_user",
30-
"CommonName": "User",
30+
"ClassName": "User",
3131
"Type": "ServiceNow.UserAndUserGroup"
3232
},
3333
{
3434
"Name": "sys_user_group",
35-
"CommonName": "User Group",
35+
"ClassName": "User Group",
3636
"Type": "ServiceNow.UserAndUserGroup"
37+
},
38+
{
39+
"Name": "sc_task",
40+
"ClassName": "Catalog Task",
41+
"Type": "ServiceNow.CatalogTask"
3742
}
3843
],
3944
"FilterOperators": [

0 commit comments

Comments
 (0)