Skip to content

Commit 5aa4456

Browse files
authored
Merge pull request #87 from Dejulia489/development
New-ServiceNowChangeRequest.ps1
2 parents ccc346a + c0bdc52 commit 5aa4456

File tree

2 files changed

+196
-1
lines changed

2 files changed

+196
-1
lines changed
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
function New-ServiceNowChangeRequest {
2+
<#
3+
.SYNOPSIS
4+
Generates a new ServiceNow change request
5+
6+
.DESCRIPTION
7+
Generates a new ServiceNow change request using predefined or custom fields by invoking the ServiceNow API
8+
9+
.PARAMETER Caller
10+
sys_id of the caller of the change request (user Get-ServiceNowUser to retrieve this)
11+
12+
.PARAMETER ShortDescription
13+
Short description of the change request
14+
15+
.PARAMETER Description
16+
Long description of the change request
17+
18+
.PARAMETER AssignmentGroup
19+
sys_id of the assignment group (use Get-ServiceNowUserGroup to retrieve this)
20+
21+
.PARAMETER Comment
22+
Comment to include in the ticket
23+
24+
.PARAMETER Category
25+
Category of the change request (e.g. 'Network')
26+
27+
.PARAMETER Subcategory
28+
Subcategory of the change request (e.g. 'Network')
29+
30+
.PARAMETER ConfigurationItem
31+
sys_id of the configuration item of the change request
32+
33+
.PARAMETER CustomFields
34+
Custom fields as hashtable
35+
36+
.PARAMETER ServiceNowCredential
37+
Credential used to authenticate to ServiceNow
38+
39+
.PARAMETER ServiceNowURL
40+
The URL for the ServiceNow instance being used (eg: instancename.service-now.com)
41+
42+
.PARAMETER Connection
43+
Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
44+
45+
.PARAMETER PassThru
46+
Returns the ticket values after creation
47+
48+
.LINK
49+
https://github.com/Sam-Martin/servicenow-powershell
50+
51+
.EXAMPLE
52+
Generate a basic change request attributed to the caller "UserName" with descriptions, categories, assignment groups and CMDB items set.
53+
54+
New-ServiceNowchange request -Caller UserName -ShortDescription 'New PS change request' -Description 'This change request was created from Powershell' -AssignmentGroup ServiceDesk -Comment 'Inline Comment' -Category Office -Subcategory Outlook -ConfigurationItem UserPC1
55+
56+
.EXAMPLE
57+
Generate an Change Request by 'splatting' all fields used in the 1st example plus some additional custom ServiceNow fields (These must exist in your ServiceNow instance), This example uses the caller's sys_id value for identification.
58+
59+
$newServiceNowChangeRequestSplat = @{
60+
Caller = '55ccf91161924edc979d8e7e5627a47d'
61+
ShortDescription = 'New PS Change Request'
62+
Description = 'This change request was created from Powershell'
63+
AssignmentGroup = 'ServiceDesk'
64+
Comment = 'Inline Comment'
65+
Category = 'Office'
66+
Subcategory = 'Outlook'
67+
ConfigurationItem = 'UserPC1'
68+
CustomFields = @{
69+
u_custom1 = 'Custom Field Entry'
70+
u_another_custom = 'Related Test'
71+
}
72+
}
73+
New-ServiceNowChangeRequest @newServiceNowChangeRequestSplat
74+
#>
75+
76+
[CmdletBinding(DefaultParameterSetName, SupportsShouldProcess)]
77+
Param(
78+
[parameter(Mandatory = $true)]
79+
[string]$Caller,
80+
81+
[parameter(Mandatory = $true)]
82+
[string]$ShortDescription,
83+
84+
[parameter(Mandatory = $false)]
85+
[string]$Description,
86+
87+
[parameter(Mandatory = $false)]
88+
[string]$AssignmentGroup,
89+
90+
[parameter(Mandatory = $false)]
91+
[string]$Comment,
92+
93+
[parameter(Mandatory = $false)]
94+
[string]$Category,
95+
96+
[parameter(Mandatory = $false)]
97+
[string]$Subcategory,
98+
99+
[parameter(Mandatory = $false)]
100+
[string]$ConfigurationItem,
101+
102+
[parameter(Mandatory = $false)]
103+
[hashtable]$CustomFields,
104+
105+
[Parameter(ParameterSetName = 'SpecifyConnectionFields', Mandatory = $True)]
106+
[ValidateNotNullOrEmpty()]
107+
[PSCredential]$ServiceNowCredential,
108+
109+
[Parameter(ParameterSetName = 'SpecifyConnectionFields', Mandatory = $True)]
110+
[ValidateNotNullOrEmpty()]
111+
[string]$ServiceNowURL,
112+
113+
[Parameter(ParameterSetName = 'UseConnectionObject', Mandatory = $True)]
114+
[ValidateNotNullOrEmpty()]
115+
[Hashtable]$Connection,
116+
117+
# Switch to allow the results to be passed back
118+
[Parameter(Mandatory = $false)]
119+
[switch]$PassThru
120+
)
121+
122+
begin { }
123+
process {
124+
Try {
125+
# Create a hash table of any defined parameters (not CustomFields) that have values
126+
$DefinedChangeRequestParameters = @('AssignmentGroup', 'Caller', 'Category', 'Comment', 'ConfigurationItem', 'Description', 'ShortDescription', 'Subcategory')
127+
$TableEntryValues = @{ }
128+
ForEach ($Parameter in $DefinedChangeRequestParameters) {
129+
If ($null -ne $PSBoundParameters.$Parameter) {
130+
# Turn the defined parameter name into the ServiceNow attribute name
131+
$KeyToAdd = Switch ($Parameter) {
132+
AssignmentGroup { 'assignment_group' }
133+
Caller { 'caller_id' }
134+
Category { 'category' }
135+
Comment { 'comments' }
136+
ConfigurationItem { 'cmdb_ci' }
137+
Description { 'description' }
138+
ShortDescription { 'short_description' }
139+
Subcategory { 'subcategory' }
140+
}
141+
$TableEntryValues.Add($KeyToAdd, $PSBoundParameters.$Parameter)
142+
}
143+
}
144+
145+
# Add CustomFields hash pairs to the Table Entry Values hash table
146+
If ($null -ne $PSBoundParameters.CustomFields) {
147+
$DuplicateTableEntryValues = ForEach ($Key in $CustomFields.Keys) {
148+
If (($TableEntryValues.ContainsKey($Key) -eq $False)) {
149+
# Add the unique entry to the table entry values hash table
150+
$TableEntryValues.Add($Key, $CustomFields[$Key])
151+
}
152+
Else {
153+
# Capture the duplicate key name
154+
$Key
155+
}
156+
}
157+
}
158+
159+
# Throw an error if duplicate fields were provided
160+
If ($null -ne $DuplicateTableEntryValues) {
161+
$DuplicateKeyList = $DuplicateTableEntryValues -join ","
162+
Throw "Ticket fields may only be used once: $DuplicateKeyList"
163+
}
164+
165+
# Table Entry Splat
166+
$newServiceNowTableEntrySplat = @{
167+
Table = 'change_request'
168+
Values = $TableEntryValues
169+
}
170+
171+
# Update the splat if the parameters have values
172+
If ($null -ne $PSBoundParameters.Connection) {
173+
$newServiceNowTableEntrySplat.Add('Connection', $Connection)
174+
}
175+
ElseIf ($null -ne $PSBoundParameters.ServiceNowCredential -and $null -ne $PSBoundParameters.ServiceNowURL) {
176+
$newServiceNowTableEntrySplat.Add('ServiceNowCredential', $ServiceNowCredential)
177+
$newServiceNowTableEntrySplat.Add('ServiceNowURL', $ServiceNowURL)
178+
}
179+
180+
# Create the table entry
181+
If ($PSCmdlet.ShouldProcess($Uri, $MyInvocation.MyCommand)) {
182+
$Result = New-ServiceNowTableEntry @newServiceNowTableEntrySplat
183+
184+
# Option to return results
185+
If ($PSBoundParameters.ContainsKey('Passthru')) {
186+
$Result
187+
}
188+
}
189+
}
190+
Catch {
191+
Write-Error $PSItem
192+
}
193+
}
194+
end { }
195+
}

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 = @('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-ServiceNowIncident','New-ServiceNowQuery','New-ServiceNowTableEntry','Remove-ServiceNowAttachment','Remove-ServiceNowAuth','Remove-ServiceNowTableEntry','Set-ServiceNowAuth','Test-ServiceNowAuthIsSet','Update-ServiceNowChangeRequest','Update-ServiceNowIncident','Update-ServiceNowNumber','Update-ServiceNowTableEntry')
69+
FunctionsToExport = @('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-ServiceNowTableEntry')
7070

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

0 commit comments

Comments
 (0)