@@ -4,98 +4,110 @@ function New-ServiceNowChangeRequest {
44 Generates a new ServiceNow change request
55
66 . DESCRIPTION
7- Generates a new ServiceNow change request using predefined or custom fields by invoking the ServiceNow API
7+ Generates a new ServiceNow change request directly with values or via a change model or template.
8+
9+ . PARAMETER ModelID
10+ Name or sys_id of the change model to use
11+
12+ . PARAMETER TemplateID
13+ Name of sys_id of the standard change template to use
814
915 . PARAMETER Caller
10- sys_id of the caller of the change request (user Get-ServiceNowUser to retrieve this)
16+ Full name or sys_id of the caller
1117
1218 . PARAMETER ShortDescription
13- Short description of the change request
19+ Short description
1420
1521 . PARAMETER Description
16- Long description of the change request
22+ Long description
1723
1824 . PARAMETER AssignmentGroup
19- sys_id of the assignment group (use Get-ServiceNowUserGroup to retrieve this)
25+ Full name or sys_id of the assignment group
2026
2127 . PARAMETER Comment
22- Comment to include in the ticket
28+ Comment to include
2329
2430 . PARAMETER Category
25- Category of the change request (e.g. 'Network')
31+ Category name
2632
2733 . PARAMETER Subcategory
28- Subcategory of the change request (e.g. 'Network')
34+ Subcategory name
2935
3036 . PARAMETER ConfigurationItem
31- sys_id of the configuration item of the change request
37+ Full name or sys_id of the configuration item to be associated with the change
3238
33- . PARAMETER CustomFields
34- Custom fields as hashtable
39+ . PARAMETER CustomField
40+ Custom field values which aren't one of the built in function properties
3541
3642 . PARAMETER Connection
3743 Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
3844
3945 . PARAMETER PassThru
40- Returns the ticket values after creation
46+ If provided, the new record will be returned
47+
48+ . EXAMPLE
49+ New-ServiceNowChangeRequest -Caller 'Greg Brownstein' -ShortDescription 'New change request'
4150
42- . LINK
43- https://github.com/Snow-Shell/servicenow-powershell
51+ Create a basic change request
4452
4553 . EXAMPLE
46- Generate a basic change request attributed to the caller "UserName" with descriptions, categories, assignment groups and CMDB items set.
54+ New-ServiceNowChangeRequest -Caller 'Greg Brownstein' -ShortDescription 'New change request' -CustomField @{'urgency'='1'}
4755
48- 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
56+ Create a basic change request with custom fields
4957
5058 . EXAMPLE
51- 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.
52-
53- $newServiceNowChangeRequestSplat = @{
54- Caller = '55ccf91161924edc979d8e7e5627a47d'
55- ShortDescription = 'New PS Change Request'
56- Description = 'This change request was created from Powershell'
57- AssignmentGroup = 'ServiceDesk'
58- Comment = 'Inline Comment'
59- Category = 'Office'
60- Subcategory = 'Outlook'
61- ConfigurationItem = 'UserPC1'
62- CustomFields = @{
63- u_custom1 = 'Custom Field Entry'
64- u_another_custom = 'Related Test'
65- }
66- }
67- New-ServiceNowChangeRequest @newServiceNowChangeRequestSplat
59+ New-ServiceNowChangeRequest -TemplateID 'Change VLAN on a Cisco switchport - 1'
60+
61+ Create a change request from a standard change template
62+
63+ . EXAMPLE
64+ New-ServiceNowChangeRequest -ModelID 'Normal' -ShortDescription 'make this change' -ConfigurationItem dbserver1
65+
66+ Create a change request from a change model
67+
68+ . EXAMPLE
69+ New-ServiceNowChangeRequest -Caller 'Greg Brownstein' -ShortDescription 'New change request' -PassThru
70+
71+ Create a change request and return the newly created record
72+
6873 #>
6974
70- [CmdletBinding (SupportsShouldProcess )]
75+ [CmdletBinding (SupportsShouldProcess , DefaultParameterSetName = ' direct ' )]
7176
7277 Param (
73- [parameter (Mandatory )]
74- [string ]$Caller ,
78+ [parameter (Mandatory , ParameterSetName = ' model' )]
79+ [string ] $ModelID ,
80+
81+ [parameter (Mandatory , ParameterSetName = ' template' )]
82+ [string ] $TemplateID ,
83+
84+ [parameter ()]
85+ [string ] $Caller ,
7586
76- [parameter (Mandatory )]
77- [string ]$ShortDescription ,
87+ [parameter ()]
88+ [string ] $ShortDescription ,
7889
7990 [parameter ()]
80- [string ]$Description ,
91+ [string ] $Description ,
8192
8293 [parameter ()]
83- [string ]$AssignmentGroup ,
94+ [string ] $AssignmentGroup ,
8495
8596 [parameter ()]
86- [string ]$Comment ,
97+ [string ] $Comment ,
8798
8899 [parameter ()]
89- [string ]$Category ,
100+ [string ] $Category ,
90101
91102 [parameter ()]
92- [string ]$Subcategory ,
103+ [string ] $Subcategory ,
93104
94105 [parameter ()]
95- [string ]$ConfigurationItem ,
106+ [string ] $ConfigurationItem ,
96107
97108 [parameter ()]
98- [hashtable ]$CustomFields ,
109+ [Alias (' CustomFields' )]
110+ [hashtable ] $CustomField ,
99111
100112 [Parameter ()]
101113 [Hashtable ] $Connection ,
@@ -110,58 +122,48 @@ function New-ServiceNowChangeRequest {
110122 begin {}
111123
112124 process {
113- # Create a hash table of any defined parameters (not CustomFields) that have values
114- $DefinedChangeRequestParameters = @ (' AssignmentGroup' , ' Caller' , ' Category' , ' Comment' , ' ConfigurationItem' , ' Description' , ' ShortDescription' , ' Subcategory' )
115- $TableEntryValues = @ { }
116- ForEach ($Parameter in $DefinedChangeRequestParameters ) {
117- If ($null -ne $PSBoundParameters .$Parameter ) {
118- # Turn the defined parameter name into the ServiceNow attribute name
119- $KeyToAdd = Switch ($Parameter ) {
120- AssignmentGroup { ' assignment_group' ; break }
121- Caller { ' caller_id' ; break }
122- Category { ' category' ; break }
123- Comment { ' comments' ; break }
124- ConfigurationItem { ' cmdb_ci' ; break }
125- Description { ' description' ; break }
126- ShortDescription { ' short_description' ; break }
127- Subcategory { ' subcategory' ; break }
128- }
129- $TableEntryValues.Add ($KeyToAdd , $PSBoundParameters .$Parameter )
130- }
125+
126+ $values = @ {}
127+ Switch ($PSBoundParameters.Keys ) {
128+ AssignmentGroup { $values [' assignment_group' ] = $PSBoundParameters.AssignmentGroup }
129+ Caller { $values [' caller_id' ] = $PSBoundParameters.Caller }
130+ Category { $values [' category' ] = $PSBoundParameters.Category }
131+ Comment { $values [' comments' ] = $PSBoundParameters.Comment }
132+ ConfigurationItem { $values [' cmdb_ci' ] = $PSBoundParameters.ConfigurationItem }
133+ Description { $values [' description' ] = $PSBoundParameters.Description }
134+ ShortDescription { $values [' short_description' ] = $PSBoundParameters.ShortDescription }
135+ Subcategory { $values [' subcategory' ] = $PSBoundParameters.Subcategory }
136+ ModelID { $values [' chg_model' ] = $PSBoundParameters.ModelID }
137+ TemplateID { $values [' std_change_producer_version' ] = $PSBoundParameters.TemplateID ; $values [' type' ] = ' Standard' }
131138 }
132139
133- # Add CustomFields hash pairs to the Table Entry Values hash table
134- If ($null -ne $PSBoundParameters.CustomFields ) {
135- $DuplicateTableEntryValues = ForEach ($Key in $CustomFields.Keys ) {
136- If (($TableEntryValues.ContainsKey ($Key ) -eq $False )) {
137- # Add the unique entry to the table entry values hash table
138- $TableEntryValues.Add ($Key , $CustomFields [$Key ])
139- }
140- Else {
141- # Capture the duplicate key name
142- $Key
143- }
140+ # add custom fields
141+ $duplicateValues = ForEach ($Key in $CustomField.Keys ) {
142+ If ( $values.ContainsKey ($Key ) ) {
143+ $Key
144+ }
145+ Else {
146+ $values.Add ($Key , $CustomField [$Key ])
144147 }
145148 }
146149
147150 # Throw an error if duplicate fields were provided
148- If ($null -ne $DuplicateTableEntryValues ) {
149- $DuplicateKeyList = $DuplicateTableEntryValues -join " ,"
150- Throw " Ticket fields may only be used once: $DuplicateKeyList "
151+ If ( $duplicateValues ) {
152+ Throw (' Fields may only be used once and the following were duplicated: {0}' -f $duplicateValues -join " ," )
151153 }
152154
153155 # Table Entry Splat
154156 $params = @ {
155- Method = ' Post'
156157 Table = ' change_request'
157- Values = $TableEntryValues
158+ Values = $values
158159 Connection = $Connection
159160 ServiceNowSession = $ServiceNowSession
161+ PassThru = $true
160162 }
161163
162- If ( $PSCmdlet.ShouldProcess ($ShortDescription , ' Create new change request' ) ) {
163- $response = Invoke-ServiceNowRestMethod @params
164- If ($PassThru.IsPresent ) {
164+ If ( $PSCmdlet.ShouldProcess (' ' , ' Create new change request' ) ) {
165+ $response = New-ServiceNowRecord @params
166+ If ( $PassThru ) {
165167 $response.PSObject.TypeNames.Insert (0 , " ServiceNow.ChangeRequest" )
166168 $response
167169 }
0 commit comments