Skip to content

Commit 0c1bc76

Browse files
committed
Split out into multiple files
Added * Get-ServiceNowConfigurationItem * Get-ServiceNowUser * Get-ServiceNowUserGroup * New-ServiceNowIncident * New-ServiceNowTableEntry * Remove-ServiceNowAuth * Remove-ServiceNowTableEntry Exposed: * Test-ServiceNowAuthIsSet * New-ServiceNowQuery
1 parent 6f16ffa commit 0c1bc76

8 files changed

+402
-96
lines changed

Readme.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,18 @@ Get-ServiceNowIncident -MatchContains @{short_description='PowerShell'}
1818
```
1919

2020
## Cmdlets
21+
* Get-ServiceNowConfigurationItem
2122
* Get-ServiceNowIncident
2223
* Get-ServiceNowTable
24+
* Get-ServiceNowUser
25+
* Get-ServiceNowUserGroup
26+
* New-ServiceNowIncident
27+
* New-ServiceNowQuery
28+
* New-ServiceNowTableEntry
29+
* Remove-ServiceNowAuth
30+
* Remove-ServiceNowTableEntry
2331
* Set-ServiceNowAuth
32+
* Test-ServiceNowAuthIsSet
2433

2534
## Tests
2635
This module comes with [Pester](https://github.com/pester/Pester/) tests for unit testing.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
function Get-ServiceNowConfigurationItem {
2+
param(
3+
# Machine name of the field to order by
4+
[parameter(mandatory=$false)]
5+
[string]$OrderBy='name',
6+
7+
# Direction of ordering (Desc/Asc)
8+
[parameter(mandatory=$false)]
9+
[ValidateSet("Desc", "Asc")]
10+
[string]$OrderDirection='Desc',
11+
12+
# Maximum number of records to return
13+
[parameter(mandatory=$false)]
14+
[int]$Limit=10,
15+
16+
# Hashtable containing machine field names and values returned must match exactly (will be combined with AND)
17+
[parameter(mandatory=$false)]
18+
[hashtable]$MatchExact=@{},
19+
20+
# Hashtable containing machine field names and values returned rows must contain (will be combined with AND)
21+
[parameter(mandatory=$false)]
22+
[hashtable]$MatchContains=@{},
23+
24+
# Whether or not to show human readable display values instead of machine values
25+
[parameter(mandatory=$false)]
26+
[ValidateSet("true","false", "all")]
27+
[string]$DisplayValues='true'
28+
)
29+
30+
$Query = New-ServiceNowQuery -OrderBy $OrderBy -OrderDirection $OrderDirection -MatchExact $MatchExact -MatchContains $MatchContains
31+
32+
$result = Get-ServiceNowTable -Table 'cmdb_ci' -Query $Query -Limit $Limit -DisplayValues $DisplayValues;
33+
34+
# Set the default property set for the table view
35+
$DefaultProperties = @('name', 'category', 'subcategory')
36+
$DefaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet(DefaultDisplayPropertySet,[string[]]$DefaultProperties)
37+
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($DefaultDisplayPropertySet)
38+
$Result | Add-Member MemberSet PSStandardMembers $PSStandardMembers
39+
return $result
40+
}

ServiceNow-Incidents.psm1

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<#
2+
.Synopsis
3+
Returns incidents from the connected ServiceNow instance based (optionally based on criteria)
4+
.NOTES
5+
You must have invoked Set-ServiceNowAuth prior to executing this cmdlet
6+
.EXAMPLE
7+
Return the incident whose number is exactly INC0010683
8+
Get-ServiceNowIncident -MatchExact @{number='INC0010683'}
9+
.EXAMPLE
10+
Return all incidents where the short description contains the word 'user'
11+
Get-ServiceNowIncident -MatchContains @{short_description='user'}
12+
#>
13+
14+
function Get-ServiceNowIncident{
15+
param(
16+
# Machine name of the field to order by
17+
[parameter(mandatory=$false)]
18+
[string]$OrderBy='opened_at',
19+
20+
# Direction of ordering (Desc/Asc)
21+
[parameter(mandatory=$false)]
22+
[ValidateSet("Desc", "Asc")]
23+
[string]$OrderDirection='Desc',
24+
25+
# Maximum number of records to return
26+
[parameter(mandatory=$false)]
27+
[int]$Limit=10,
28+
29+
# Hashtable containing machine field names and values returned must match exactly (will be combined with AND)
30+
[parameter(mandatory=$false)]
31+
[hashtable]$MatchExact=@{},
32+
33+
# Hashtable containing machine field names and values returned rows must contain (will be combined with AND)
34+
[parameter(mandatory=$false)]
35+
[hashtable]$MatchContains=@{},
36+
37+
# Whether or not to show human readable display values instead of machine values
38+
[parameter(mandatory=$false)]
39+
[ValidateSet("true","false", "all")]
40+
[string]$DisplayValues='true'
41+
)
42+
43+
$Query = New-ServiceNowQuery -OrderBy $OrderBy -OrderDirection $OrderDirection -MatchExact $MatchExact -MatchContains $MatchContains
44+
45+
$result = Get-ServiceNowTable -Table 'incident' -Query $Query -Limit $Limit -DisplayValues $DisplayValues;
46+
47+
# Set the default property set for the table view
48+
$DefaultProperties = @('number', 'short_description', 'opened_at')
49+
$DefaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet(DefaultDisplayPropertySet,[string[]]$DefaultProperties)
50+
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($DefaultDisplayPropertySet)
51+
$Result | Add-Member MemberSet PSStandardMembers $PSStandardMembers
52+
53+
# Return that result!
54+
return $result
55+
}
56+
57+
<#
58+
.EXAMPLE
59+
New-ServiceNowIncident -ShortDescription "Testing with Pester" `
60+
-Description "Long description" -AssignmentGroup "e9e9a2406f4c35001855fa0dba3ee4f3" `
61+
-Category "Internal" -SubCategory "Task" `
62+
-Comment "Comment" -ConfigurationItem "bee8e0ed6f8475001855fa0dba3ee4ea" `
63+
-Caller "7a4b573a6f3725001855fa0dba3ee485" `
64+
#>
65+
66+
function New-ServiceNowIncident{
67+
Param(
68+
69+
# sys_id of the caller of the incident (user Get-ServiceNowUser to retrieve this)
70+
[parameter(mandatory=$true)]
71+
[string]$Caller,
72+
73+
# Short description of the incident
74+
[parameter(mandatory=$true)]
75+
[string]$ShortDescription,
76+
77+
# Long description of the incident
78+
[parameter(mandatory=$false)]
79+
[string]$Description,
80+
81+
# sys_id of the assignment group (use Get-ServiceNowUserGroup to retrieve this)
82+
[parameter(mandatory=$false)]
83+
[string]$AssignmentGroup,
84+
85+
# Comment to include in the ticket
86+
[parameter(mandatory=$false)]
87+
[string]$Comment,
88+
89+
# Category of the incident (e.g. 'Network')
90+
[parameter(mandatory=$false)]
91+
[string]$Category,
92+
93+
# Subcategory of the incident (e.g. 'Network')
94+
[parameter(mandatory=$false)]
95+
[string]$Subcategory,
96+
97+
# sys_id of the configuration item of the incident
98+
[parameter(mandatory=$false)]
99+
[string]$ConfigurationItem
100+
)
101+
102+
$Values = @{
103+
'caller_id' = $Caller
104+
'short_description' = $ShortDescription
105+
'description' = $Description
106+
'assignment_group' = $AssignmentGroup
107+
'comments' = $Comment
108+
'category' = $Category
109+
'subcategory' = $Subcategory
110+
'cmdb_ci' = $ConfigurationItem
111+
}
112+
113+
New-ServiceNowTableEntry -Table 'incident' -Values $Values
114+
115+
}
116+

ServiceNow-Module.Tests.ps1

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,54 @@ if(Test-Path $DefaultsFile){
1616
# Write example file
1717
@{
1818
ServiceNowURL = 'testingurl.service-now.com'
19+
TestCategory = 'Internal'
20+
TestUserGroup = 'e9e9a2406f4c35001855fa0dba3ee4f3'
1921
} | ConvertTo-Json | Set-Content $DefaultsFile
2022
return;
2123
}
2224

2325
# Load the module (unload it first in case we've made changes since loading it previously)
2426
Remove-Module ServiceNow-Module -ErrorAction SilentlyContinue
25-
Import-Module $here\ServiceNow-Module.psm1
27+
Import-Module $here\ServiceNow-Module.psd1
2628

2729
Describe "ServiceNow-Module" {
2830

2931
It "Set-ServiceNowAuth works" {
3032
Set-ServiceNowAuth -url $defaults.ServiceNowURL -Credentials $defaults.Creds | Should be $true
3133
}
3234

35+
It "New-ServiceNowIncident (and by extension New-ServiceNowTableEntry) works" {
36+
$TestTicket = New-ServiceNowIncident -ShortDescription "Testing with Pester" `
37+
-Description "Long description" -AssignmentGroup $defaults.TestUserGroup `
38+
-Category $defaults.TestCategory -SubCategory $Defaults.TestSubcategory `
39+
-Comment "Comment" -ConfigurationItem $defaults.TestConfigurationItem `
40+
-Caller $defaults.TestUser `
41+
42+
$TestTicket.short_description | Should be "Testing with Pester"
43+
}
44+
3345
It "Get-ServiceNowTable works" {
3446
# There should be one or more incidents returned
35-
(Get-ServiceNowTable -Table 'incident' -Query 'ORDERBYDESCopened_at').Count | Should Match '\d?'
47+
(Get-ServiceNowTable -Table 'incident' -Query 'ORDERBYDESCopened_at').Count -gt 0 | Should Match $true
3648
}
3749

3850
It "Get-ServiceNowIncident works" {
3951
# There should be one or more incidents returned
40-
(Get-ServiceNowIncident).Count | Should Match '\d?'
52+
(Get-ServiceNowIncident).Count -gt 0 | Should Match $true
53+
}
54+
55+
It "Get-ServiceNowUserGroup works" {
56+
# There should be one or more user groups returned
57+
(Get-ServiceNowUserGroup).Count -gt 0 | Should Match $true
58+
}
59+
60+
It "Get-ServiceNowUser works" {
61+
# There should be one or more user groups returned
62+
(Get-ServiceNowUser).Count -gt 0 | Should Match $true
63+
}
64+
65+
It "Get-ServiceNowConfigurationItem works" {
66+
# There should be one or more configuration items returned
67+
(Get-ServiceNowConfigurationItem).Count -gt 0 | Should Match $true
4168
}
4269
}

ServiceNow-Module.psd1

122 Bytes
Binary file not shown.

ServiceNow-Module.psm1

Lines changed: 35 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,31 @@
1-
function CheckServiceNowAuthIsSet{
2-
if($script:ServiceNowCredentials){
1+
function Test-ServiceNowAuthIsSet{
2+
if($Global:ServiceNowCredentials){
33
return $true;
44
}else{
55
return $false;
66
}
77
}
88

9-
function Set-ServiceNowAuth{
10-
param(
11-
[parameter(mandatory=$true)]
12-
[string]$url,
13-
14-
[parameter(mandatory=$true)]
15-
[System.Management.Automation.PSCredential]$Credentials
16-
)
17-
$script:ServiceNowURL = 'https://' + $url
18-
$script:ServiceNowRESTURL = $ServiceNowURL + '/api/now/v1'
19-
$script:ServiceNowCredentials = $credentials
20-
return $true;
21-
}
22-
23-
function Get-ServiceNowTable
24-
{
25-
[OutputType([Array])]
26-
Param
27-
(
28-
# Name of the table we're querying (e.g. incidents)
29-
[parameter(mandatory=$true)]
30-
[string]$Table,
31-
32-
# sysparm_query param in the format of a ServiceNow encoded query string (see http://wiki.servicenow.com/index.php?title=Encoded_Query_Strings)
33-
[parameter(mandatory=$false)]
34-
[string]$Query,
35-
36-
# Maximum number of records to return
37-
[parameter(mandatory=$false)]
38-
[int]$Limit=10,
39-
40-
# Whether or not to show human readable display values instead of machine values
41-
[parameter(mandatory=$false)]
42-
[ValidateSet("true","false", "all")]
43-
[string]$DisplayValues='false'
44-
)
45-
46-
if(! (CheckServiceNowAuthIsSet)){
47-
Write-Error "You must run Set-ServiceNowAuth prior to executing this cmdlet in order to provide credentials"
48-
}
49-
50-
# Populate the query
51-
$Body = @{'sysparm_limit'=$Limit;'sysparm_display_value'=$DisplayValues}
52-
if($Query){
53-
$Body.sysparm_query = $Query
54-
}
55-
56-
# Fire and return
57-
$Uri = $script:ServiceNowRESTURL + "/table/$Table"
58-
return (Invoke-RestMethod -Uri $uri -Credential $script:ServiceNowCredentials -Body $Body -ContentType "application/json").result
59-
}
60-
61-
<#
62-
.Synopsis
63-
Returns incidents from the connected ServiceNow instance based (optionally based on criteria)
64-
.NOTES
65-
You must have invoked Set-ServiceNowAuth prior to executing this cmdlet
66-
.EXAMPLE
67-
Return the incident whose number is exactly INC0010683
68-
Get-ServiceNowIncident -MatchExact @{number='INC0010683'}
69-
.EXAMPLE
70-
Return all incidents where the short description contains the word 'user'
71-
Get-ServiceNowIncident -MatchContains @{short_description='user'}
72-
#>
9+
function New-ServiceNowQuery{
7310

74-
function Get-ServiceNowIncident{
7511
param(
7612
# Machine name of the field to order by
77-
[parameter(mandatory=$false)]
13+
[parameter(mandatory=$true)]
7814
[string]$OrderBy='opened_at',
7915

8016
# Direction of ordering (Desc/Asc)
81-
[parameter(mandatory=$false)]
17+
[parameter(mandatory=$true)]
8218
[ValidateSet("Desc", "Asc")]
8319
[string]$OrderDirection='Desc',
84-
85-
# Maximum number of records to return
86-
[parameter(mandatory=$false)]
87-
[int]$Limit=10,
8820

8921
# Hashtable containing machine field names and values returned must match exactly (will be combined with AND)
90-
[parameter(mandatory=$false)]
22+
[parameter(mandatory=$true)]
9123
[hashtable]$MatchExact,
9224

9325
# Hashtable containing machine field names and values returned rows must contain (will be combined with AND)
94-
[parameter(mandatory=$false)]
95-
[hashtable]$MatchContains,
96-
97-
# Whether or not to show human readable display values instead of machine values
98-
[parameter(mandatory=$false)]
99-
[ValidateSet("true","false", "all")]
100-
[string]$DisplayValues='true'
26+
[parameter(mandatory=$true)]
27+
[hashtable]$MatchContains
10128
)
102-
10329
# Start the query off with a order direction
10430
$Query = '';
10531
if($OrderDirection -eq 'Asc'){
@@ -122,17 +48,33 @@ function Get-ServiceNowIncident{
12248
$Query += "^$($Field)LIKE"+$MatchContains.$Field
12349
}
12450
}
125-
126-
$result = Get-ServiceNowTable -Table 'incident' -Query $Query -Limit $Limit -DisplayValues $DisplayValues;
127-
128-
# Set the default property set for the table view
129-
$DefaultProperties = @('number', 'short_description', 'opened_at')
130-
$DefaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet(DefaultDisplayPropertySet,[string[]]$DefaultProperties)
131-
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($DefaultDisplayPropertySet)
132-
$Result | Add-Member MemberSet PSStandardMembers $PSStandardMembers
13351

134-
# Return that result!
135-
return $result
52+
return $Query
53+
}
54+
55+
function Set-ServiceNowAuth{
56+
param(
57+
[parameter(mandatory=$true)]
58+
[string]$url,
59+
60+
[parameter(mandatory=$true)]
61+
[System.Management.Automation.PSCredential]$Credentials
62+
)
63+
$Global:ServiceNowURL = 'https://' + $url
64+
$Global:ServiceNowRESTURL = $ServiceNowURL + '/api/now/v1'
65+
$Global:ServiceNowCredentials = $credentials
66+
return $true;
13667
}
13768

138-
#http://wiki.servicenow.com/index.php?title=Tables_and_Classes#gsc.tab=0
69+
<#
70+
.SYNOPSIS
71+
Cleans up the variables containing your authentication information from your PowerShell session
72+
#>
73+
function Remove-ServiceNowAuth{
74+
75+
Remove-Variable -Name ServiceNowURL -Scope Global
76+
Remove-Variable -Name ServiceNowRESTURL -Scope Global
77+
Remove-Variable -Name ServiceNowCredentials -Scope Global
78+
79+
return $true;
80+
}

0 commit comments

Comments
 (0)