├── README.md ├── WC-ServiceNow.psd1 └── WC-ServiceNow.psm1 /README.md: -------------------------------------------------------------------------------- 1 | # PSServiceNow 2 | PowerShell Functions for the ServiceNow API 3 | 4 | ![alt text](https://github.com/hematic/Storage/raw/master/servicenow.jpg) 5 | 6 | # Requirements 7 | - Know the URL for your ServiceNow Instance 8 | - Valid Credentials for your ServiceNow instance. 9 | - PowerShell 3.0 or greater 10 | # Features 11 | - Retrieval of a ServiceNow user 12 | - By Samaccountname 13 | - By Email Address 14 | - By First and Last Name 15 | - Retrieval of ServiceNow Incident 16 | - By IncidentID 17 | - By Description 18 | - By ServiceNow SYSUserID 19 | - By Assigned to Email Address 20 | - By First and Last name of assigned user 21 | - Create a ServiceNow Incident 22 | - Passing of almost any field is available however its possible your organization has customized your incident form to use different field names. If so you may need to customize this function to use the fields your organization uses. To do so just simply change the function parameter names. The help example shows the fields available. 23 | - By Iteration Name 24 | - Update a ServiceNow Incident 25 | - Passing of almost any field is available however its possible your organization has customized your incident form to use different field names. If so you may need to customize this function to use the fields your organization uses. To do so just simply change the function parameter names. The help example shows the fields available. 26 | 27 | # Examples and Help 28 | 29 | Currently located inside the functions themselves. 30 | -------------------------------------------------------------------------------- /WC-ServiceNow.psd1: -------------------------------------------------------------------------------- 1 | # 2 | # Module manifest for module 'WC-ServiceNow' 3 | # 4 | # Generated by: Phillip Marshall 5 | # 6 | # Generated on: 10/15/2018 7 | # 8 | 9 | @{ 10 | 11 | # Script module or binary module file associated with this manifest. 12 | RootModule = '.\PSServiceNow.psm1' 13 | 14 | # Version number of this module. 15 | ModuleVersion = '1.4.4' 16 | 17 | # Supported PSEditions 18 | # CompatiblePSEditions = @() 19 | 20 | # ID used to uniquely identify this module 21 | GUID = '29e00301-6672-4e65-a833-14be2774e980' 22 | 23 | # Author of this module 24 | Author = 'Phillip Marshall' 25 | 26 | # Company or vendor of this module 27 | CompanyName = 'N/A' 28 | 29 | # Copyright statement for this module 30 | Copyright = '(c) 2018 Phillip Marshall. All rights reserved.' 31 | 32 | # Description of the functionality provided by this module 33 | Description = 'ServiceNow Ticketing Module' 34 | 35 | # Minimum version of the Windows PowerShell engine required by this module 36 | # PowerShellVersion = '' 37 | 38 | # Name of the Windows PowerShell host required by this module 39 | # PowerShellHostName = '' 40 | 41 | # Minimum version of the Windows PowerShell host required by this module 42 | # PowerShellHostVersion = '' 43 | 44 | # Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only. 45 | # DotNetFrameworkVersion = '' 46 | 47 | # Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only. 48 | # CLRVersion = '' 49 | 50 | # Processor architecture (None, X86, Amd64) required by this module 51 | # ProcessorArchitecture = '' 52 | 53 | # Modules that must be imported into the global environment prior to importing this module 54 | # RequiredModules = @() 55 | 56 | # Assemblies that must be loaded prior to importing this module 57 | # RequiredAssemblies = @() 58 | 59 | # Script files (.ps1) that are run in the caller's environment prior to importing this module. 60 | # ScriptsToProcess = @() 61 | 62 | # Type files (.ps1xml) to be loaded when importing this module 63 | # TypesToProcess = @() 64 | 65 | # Format files (.ps1xml) to be loaded when importing this module 66 | # FormatsToProcess = @() 67 | 68 | # Modules to import as nested modules of the module specified in RootModule/ModuleToProcess 69 | # NestedModules = @() 70 | 71 | # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. 72 | FunctionsToExport = @('Get-WCServiceNowIncident','Get-WCServiceNowUser','New-WCServiceNowIncident','Set-WCServiceNowIncident') 73 | 74 | # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. 75 | CmdletsToExport = '*' 76 | 77 | # Variables to export from this module 78 | VariablesToExport = '*' 79 | 80 | # Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. 81 | AliasesToExport = '*' 82 | 83 | # DSC resources to export from this module 84 | # DscResourcesToExport = @() 85 | 86 | # List of all modules packaged with this module 87 | # ModuleList = @() 88 | 89 | # List of all files packaged with this module 90 | # FileList = @() 91 | 92 | # Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell. 93 | PrivateData = @{ 94 | 95 | PSData = @{ 96 | 97 | # Tags applied to this module. These help with module discovery in online galleries. 98 | # Tags = @() 99 | 100 | # A URL to the license for this module. 101 | # LicenseUri = '' 102 | 103 | # A URL to the main website for this project. 104 | # ProjectUri = '' 105 | 106 | # A URL to an icon representing this module. 107 | # IconUri = '' 108 | 109 | # ReleaseNotes of this module 110 | ReleaseNotes = 'Added incident_state to the fields available for Set-WCServniceNowIncident' 111 | 112 | } # End of PSData hashtable 113 | 114 | } # End of PrivateData hashtable 115 | 116 | # HelpInfo URI of this module 117 | # HelpInfoURI = '' 118 | 119 | # Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix. 120 | # DefaultCommandPrefix = '' 121 | 122 | } 123 | 124 | -------------------------------------------------------------------------------- /WC-ServiceNow.psm1: -------------------------------------------------------------------------------- 1 | Function Get-WCServiceNowIncident{ 2 | <# 3 | .SYNOPSIS 4 | Function to retrieve one or more ServiceNow Incident 5 | .DESCRIPTION 6 | This function allows you query the ServiceNow REST API using various filters to retrieve one or more incidents. 7 | .EXAMPLE 8 | $Splat = @{ 9 | Credential = $Credential 10 | IncidentID = 'INC0010165' 11 | baseURI = "company.service-now.com" 12 | } 13 | Get-WCServiceNowIncident @Splat #Retrieve incident by incident ID 14 | .EXAMPLE 15 | $Splat = @{ 16 | Credential = $Credential 17 | Description = 'Test' 18 | baseURI = "company.service-now.com" 19 | } 20 | Get-WCServiceNowIncident @Splat #Retrieve incident(s) by a short description filter. 21 | .EXAMPLE 22 | $Splat = @{ 23 | Credential = $Credential 24 | User = '1a059879db9c2340873162eb0b961993' 25 | baseURI = "company.service-now.com" 26 | } 27 | Get-WCServiceNowIncident @Splat #Retrieve incident(s) filtering by ServiceNow sys user id. 28 | .EXAMPLE 29 | $Splat = @{ 30 | Credential = $Credential 31 | User = 'username' 32 | baseURI = "company.service-now.com" 33 | } 34 | $BySamaccountname = Get-WCServiceNowIncident @Splat #Retrieve incident(s) filtering by samaccountname 35 | .EXAMPLE 36 | $Splat = @{ 37 | Credential = $Credential 38 | User = 'first.last@company.com' 39 | baseURI = "company.service-now.com" 40 | } 41 | $ByEmail = Get-WCServiceNowIncident @Splat #Retrieve incident(s) filtering by email 42 | .EXAMPLE 43 | $Splat = @{ 44 | Credential = $Credential 45 | User = 'Last, First' 46 | baseURI = "company.service-now.com" 47 | } 48 | $ByFirstandLast = Get-WCServiceNowIncident @Splat #Retrieve incident(s) filtering by First and last name 49 | #> 50 | [CmdletBinding(SupportsShouldProcess=$False,ConfirmImpact='Low',DefaultParameterSetName='ByID')] 51 | 52 | Param( 53 | [Parameter(Mandatory=$True,HelpMessage='This is credential for connecting to Service Now.')] 54 | [ValidateNotNullOrEmpty()] 55 | [PSCredential]$Credential, 56 | 57 | [Parameter(Mandatory=$True,HelpMessage='This is base URI of your Service Now instance.')] 58 | [ValidateNotNullOrEmpty()] 59 | [PSCredential]$baseURI, 60 | 61 | [Parameter(Mandatory=$True,ParameterSetName = 'ByIncidentID', HelpMessage='This is Ticket ID to search for.')] 62 | [ValidateNotNullOrEmpty()] 63 | [String]$IncidentID, 64 | 65 | [Parameter(Mandatory=$True,ParameterSetName = 'ByShortDescription', HelpMessage='This is part of the short description field to search for.')] 66 | [ValidateNotNullOrEmpty()] 67 | [String]$Description, 68 | 69 | [Parameter(Mandatory=$True,ParameterSetName = 'ByUser', HelpMessage='You can pass either the servicenow Userid, the email, the samaccountname, or the last, first names separated by commas.')] 70 | [ValidateNotNullOrEmpty()] 71 | [String]$User 72 | ) 73 | 74 | #region Get Headers 75 | Try{ 76 | $Headers = New-WCServiceNowHeaders -Credential $Credential -ErrorAction Stop 77 | } 78 | Catch{ 79 | Write-Error $_ 80 | } 81 | #endregion 82 | 83 | #region Build URI 84 | switch ($PsCmdlet.ParameterSetName) 85 | { 86 | 'ByIncidentID' { 87 | $URI = "https://$BaseURI/api/now/table/incident?sysparm_query=number%3D$IncidentID&sysparm_limit=1" 88 | } 89 | 90 | 'ByShortDescription' { 91 | $URI = "https://$baseURI/api/now/table/incident?sysparm_query=short_descriptionLIKE$Description" 92 | } 93 | 94 | 'ByUser' { 95 | If($User -like '*@*'){ 96 | Try{ 97 | $SysID = (Get-WCServiceNowUser -Credential $Credential -Email $User -ErrorAction Stop).'sys_id' 98 | } 99 | Catch{ 100 | Write-Error $_ 101 | } 102 | 103 | } 104 | ElseIf($User -like '*,*'){ 105 | $Splits = $User -split ',' 106 | $Last = $Splits[0].TrimStart().TrimEnd() 107 | $First = $Splits[1].TrimStart().TrimEnd() 108 | Try{ 109 | $SysID = (Get-WCServiceNowUser -Credential $Credential -Firstname $First -LastName $Last).'sys_id' 110 | } 111 | Catch{ 112 | Write-Error $_ 113 | } 114 | } 115 | ElseIf(Get-ADuser $User){ 116 | $SysID = (Get-WCServiceNowUser -Credential $Credential -SamaccountName $User).'sys_id' 117 | } 118 | Else{ 119 | $SysID = $User 120 | } 121 | 122 | $URI = "https://$baseURI/api/now/table/incident?sysparm_query=caller_id%3D$SysID" 123 | } 124 | } 125 | 126 | #endregion 127 | 128 | #region Send HTTP request 129 | Try{ 130 | $Response = Invoke-WebRequest -Headers $Headers -Method Get -Uri $URI -ErrorAction Stop 131 | $Obj = ($Response.content | convertFrom-JSON).result 132 | Return $Obj 133 | } 134 | Catch{ 135 | Write-error $_ 136 | } 137 | #endregion 138 | } 139 | Function Get-WCServiceNowUser{ 140 | <# 141 | .SYNOPSIS 142 | Function to retrieve a ServiceNowUser 143 | .DESCRIPTION 144 | This function is useful for retrieving the otherwise hidden sys_id of a user in ServiceNow. 145 | .EXAMPLE 146 | $Splat = @{ 147 | Credential = $Credential 148 | SamaccountName = 'username' 149 | baseURI = "company.service-now.com" 150 | } 151 | Get-WCServiceNowUser @Splat #Retrieve user by samaccountname 152 | .EXAMPLE 153 | $Splat = @{ 154 | Credential = $Credential 155 | Email = 'first.last@company.com' 156 | baseURI = "company.service-now.com" 157 | } 158 | Get-WCServiceNowUser @Splat #Retrieve user by email 159 | .EXAMPLE 160 | $Splat = @{ 161 | Credential = $Credential 162 | First = 'First' 163 | Last = 'Last' 164 | baseURI = "company.service-now.com" 165 | } 166 | Get-WCServiceNowUser @Splat #Retrieve user by first and last name 167 | #> 168 | [CmdletBinding(SupportsShouldProcess=$False,ConfirmImpact='Low',DefaultParameterSetName='BySamAccountname')] 169 | Param( 170 | [Parameter(Mandatory=$True,HelpMessage='This is credential for connecting to Service Now.')] 171 | [ValidateNotNullOrEmpty()] 172 | [PSCredential]$Credential, 173 | 174 | [Parameter(Mandatory=$True,HelpMessage='This is base URI of your Service Now instance.')] 175 | [ValidateNotNullOrEmpty()] 176 | [PSCredential]$baseURI, 177 | 178 | [Parameter(Mandatory=$True,ParameterSetName = 'BySamAccountname', HelpMessage='This is Ticket ID to search for.')] 179 | [ValidateNotNullOrEmpty()] 180 | [String]$SamaccountName, 181 | 182 | [Parameter(Mandatory=$True,ParameterSetName = 'ByEmail', HelpMessage='This is part of the short description field to search for.')] 183 | [ValidateNotNullOrEmpty()] 184 | [String]$Email, 185 | 186 | [Parameter(Mandatory=$True,ParameterSetName = 'ByFirstandLast', HelpMessage='This is the user ID in ServiceNow.')] 187 | [ValidateNotNullOrEmpty()] 188 | [String]$Firstname, 189 | 190 | [Parameter(Mandatory=$True,ParameterSetName = 'ByFirstandLast', HelpMessage='This is the user ID in ServiceNow.')] 191 | [ValidateNotNullOrEmpty()] 192 | [String]$LastName 193 | ) 194 | 195 | #region Get Headers 196 | Try{ 197 | $Headers = New-WCServiceNowHeaders -Credential $Credential -ErrorAction Stop 198 | } 199 | Catch{ 200 | Write-Error $_ 201 | } 202 | #endregion 203 | 204 | #region Build URI 205 | switch ($PsCmdlet.ParameterSetName) 206 | { 207 | 'BySamAccountname' { 208 | $URI = "https://$baseURI/api/now/table/sys_user?sysparm_query=user_name%3D$SamaccountName" 209 | } 210 | 211 | 'ByEmail' { 212 | $URIEmail = $Email -replace '@', '%40' 213 | $URI = "https://$baseURI/api/now/table/sys_user?sysparm_query=email%3D$URIEmail" 214 | } 215 | 216 | 'ByFirstandLast' { 217 | $URI = "https://$baseURI/api/now/table/sys_user?sysparm_query=first_name%3D$FirstName%5Elast_name%3D$Lastname" 218 | } 219 | } 220 | 221 | #endregion 222 | 223 | #region Send HTTP request 224 | Try{ 225 | $Response = Invoke-WebRequest -Headers $Headers -Method Get -Uri $URI -ErrorAction Stop 226 | $Obj = ($Response.content | convertFrom-JSON).result 227 | Return $Obj 228 | } 229 | Catch{ 230 | Write-error $_ 231 | } 232 | #endregion 233 | } 234 | Function New-WCServiceNowIncident{ 235 | <# 236 | .SYNOPSIS 237 | Function to create a new ServiceNow Incident 238 | .DESCRIPTION 239 | This function allows you pass as few or as many of the parameters as you would like to create a 240 | ServiceNow Incident. All are available, none are mandatory. 241 | .EXAMPLE 242 | $Splat = @{ 243 | Credential = $Credential 244 | baseURI = "company.service-now.com" 245 | category = 'Hardware' 246 | subcategory = 'Desktop' 247 | u_symptom = 'Crash' 248 | location = 'Tampa' 249 | contact_type = 'Email' 250 | impact = 1 251 | urgency = 1 252 | priority = 1 253 | assignment_group = 'Service Desk' 254 | caller_id = 'username' 255 | short_description = 'This is the short description' 256 | comments = 'This is a test comment' 257 | work_notes = 'These are work notes' 258 | } 259 | New-WCServiceNowIncident @Splat #New Incident creation with all currently available fields 260 | #> 261 | [CmdletBinding(SupportsShouldProcess=$False,ConfirmImpact='Low')] 262 | Param( 263 | [Parameter(Mandatory=$True, HelpMessage='This is the API Credential.')] 264 | [ValidateNotNullOrEmpty()] 265 | [PSCredential]$Credential, 266 | 267 | [Parameter(Mandatory=$True,HelpMessage='This is base URI of your Service Now instance.')] 268 | [ValidateNotNullOrEmpty()] 269 | [PSCredential]$baseURI, 270 | 271 | [Parameter(Mandatory=$False, HelpMessage='This is the incident category.')] 272 | [ValidateSet('hardware','software_service')] 273 | [String]$category, 274 | 275 | [Parameter(Mandatory=$False, HelpMessage='This is the subcategory.')] 276 | [String]$subcategory, 277 | 278 | [Parameter(Mandatory=$False, HelpMessage='This is the symptom.')] 279 | [ValidateSet('slow_performance','error_message','crash','unable_to_launch_connect','access_issue')] 280 | [String]$u_symptom, 281 | 282 | [Parameter(Mandatory=$False, HelpMessage='This is the office location.')] 283 | [String]$location, 284 | 285 | [Parameter(Mandatory=$False, HelpMessage='This is the contact type.')] 286 | [ValidateSet('email','call','im','walk-in','non_user_query','self-service')] 287 | [String]$contact_type, 288 | 289 | [Parameter(Mandatory=$False, HelpMessage='This is the impact.')] 290 | [ValidateSet('1','2','3')] 291 | [Int]$impact, 292 | 293 | [Parameter(Mandatory=$False, HelpMessage='This is the urgency.')] 294 | [ValidateSet('1','2','3')] 295 | [Int]$urgency, 296 | 297 | [Parameter(Mandatory=$False, HelpMessage='This is the priority.')] 298 | [ValidateSet('1','2','3','4')] 299 | [Int]$priority, 300 | 301 | [Parameter(Mandatory=$False, HelpMessage='This is the assignment group.')] 302 | [String]$assignment_group, 303 | 304 | [Parameter(Mandatory=$False, HelpMessage='This is the affected user.')] 305 | [String]$caller_id, 306 | 307 | [Parameter(Mandatory=$False, HelpMessage='This is the short description which acts like a title.')] 308 | [String]$short_description, 309 | 310 | [Parameter(Mandatory=$False, HelpMessage='This is the assigned to user (not yet implemented).')] 311 | [String]$asssigned_to, 312 | 313 | [Parameter(Mandatory=$False, HelpMessage='This is a switch to suppress emails on a ticket.')] 314 | [Switch]$u_suppress_emails, 315 | 316 | [Parameter(Mandatory=$False, HelpMessage='This is the comment field that is publicly visible.')] 317 | [String]$comments, 318 | 319 | [Parameter(Mandatory=$False, HelpMessage='This is the work notes field only visible to technicians and admins.')] 320 | [String]$work_notes, 321 | 322 | [Parameter(Mandatory=$False, HelpMessage='This is the user the ticket is created on behalf of.')] 323 | [String]$u_on_behalf_of, 324 | 325 | [Parameter(Mandatory=$False, HelpMessage='This is the user the ticket is assigned to.')] 326 | [String]$assigned_to 327 | ) 328 | 329 | #region Get Headers 330 | Try{ 331 | $headers = New-WCServiceNowHeaders -Credential $Credential -ErrorAction Stop 332 | } 333 | Catch{ 334 | Write-Error $_ 335 | } 336 | #endregion 337 | 338 | #region Form Body 339 | $Body = @{} 340 | 341 | Foreach($Item in $PSBoundParameters.keys | Where-object {$_ -ne 'Credential'}){ 342 | $Body.$Item = $PSBoundParameters.Item($Item) 343 | } 344 | 345 | $jsonbody = ConvertTo-Json $Body 346 | Write-host $JSONBody 347 | #endregion 348 | 349 | #region Send HTTP request 350 | Try{ 351 | $URI = "https://$baseURI/api/now/table/incident" 352 | $Response = Invoke-WebRequest -Headers $Headers -Method Post -Uri $URI -Body $JSONBody -ContentType 'application/json' -ErrorAction Stop 353 | $Obj = ($Response.content | convertFrom-JSON).result 354 | Return $Obj 355 | } 356 | Catch{ 357 | Write-error $_ 358 | } 359 | #endregion 360 | } 361 | Function Set-WCServiceNowIncident{ 362 | <# 363 | .SYNOPSIS 364 | Function to update a new ServiceNow Incident 365 | .DESCRIPTION 366 | This function allows you pass as few or as many of the parameters as you would like to update a 367 | ServiceNow Incident. All are available, none are mandatory. 368 | .EXAMPLE 369 | $Splat = @{ 370 | Credential = $Credential 371 | baseURI = "company.service-now.com" 372 | IncidentID = 'INC0010165' 373 | impact = 2 374 | caller_id = 'username' 375 | short_description = 'This is the updated short description' 376 | comments = 'This is a second test comment' 377 | 378 | } 379 | Set-WCServiceNowIncident @Splat 380 | #> 381 | [CmdletBinding(SupportsShouldProcess=$False,ConfirmImpact='Low')] 382 | Param( 383 | [Parameter(Mandatory=$True, HelpMessage='This is the API Credential.')] 384 | [ValidateNotNullOrEmpty()] 385 | [PSCredential]$Credential, 386 | 387 | [Parameter(Mandatory=$True,HelpMessage='This is base URI of your Service Now instance.')] 388 | [ValidateNotNullOrEmpty()] 389 | [PSCredential]$baseURI, 390 | 391 | [Parameter(Mandatory=$True, HelpMessage='This is the API Credential.')] 392 | [ValidateNotNullOrEmpty()] 393 | [String]$IncidentID, 394 | 395 | [Parameter(Mandatory=$False, HelpMessage='This is the incident category.')] 396 | [ValidateSet('hardware','software_service')] 397 | [String]$category, 398 | 399 | [Parameter(Mandatory=$False, HelpMessage='This is the subcategory.')] 400 | [String]$subcategory, 401 | 402 | [Parameter(Mandatory=$False, HelpMessage='This is the symptom.')] 403 | [ValidateSet('slow_performance','error_message','crash','unable_to_launch_connect','access_issue')] 404 | [String]$u_symptom, 405 | 406 | [Parameter(Mandatory=$False, HelpMessage='This is the office location.')] 407 | [String]$location, 408 | 409 | [Parameter(Mandatory=$False, HelpMessage='This is the contact type.')] 410 | [ValidateSet('email','call','im','walk-in','non_user_query','self-service')] 411 | [String]$contact_type, 412 | 413 | [Parameter(Mandatory=$False, HelpMessage='This is the impact.')] 414 | [ValidateSet('1','2','3')] 415 | [Int]$impact, 416 | 417 | [Parameter(Mandatory=$False, HelpMessage='This is the urgency.')] 418 | [ValidateSet('1','2','3')] 419 | [Int]$urgency, 420 | 421 | [Parameter(Mandatory=$False, HelpMessage='This is the priority.')] 422 | [ValidateSet('1','2','3','4')] 423 | [Int]$priority, 424 | 425 | [Parameter(Mandatory=$False, HelpMessage='This is the assignment group.')] 426 | [String]$assignment_group, 427 | 428 | [Parameter(Mandatory=$False, HelpMessage='This is the affected user.')] 429 | [String]$caller_id, 430 | 431 | [Parameter(Mandatory=$False, HelpMessage='This is the short description which acts like a title.')] 432 | [String]$short_description, 433 | 434 | [Parameter(Mandatory=$False, HelpMessage='This is the assigned to user (not yet implemented).')] 435 | [String]$asssigned_to, 436 | 437 | [Parameter(Mandatory=$False, HelpMessage='This is a switch to suppress emails on a ticket.')] 438 | [Switch]$u_suppress_emails, 439 | 440 | [Parameter(Mandatory=$False, HelpMessage='This is the comment field that is publicly visible.')] 441 | [String]$comments, 442 | 443 | [Parameter(Mandatory=$False, HelpMessage='This is the work notes field only visible to technicians and admins.')] 444 | [String]$work_notes, 445 | 446 | [Parameter(Mandatory=$False, HelpMessage='This is the user the ticket is created on behalf of.')] 447 | [String]$u_on_behalf_of, 448 | 449 | [Parameter(Mandatory=$False, HelpMessage='This is the user the ticket is assigned to.')] 450 | [String]$assigned_to, 451 | 452 | [Parameter(Mandatory=$False, HelpMessage='This is the status of the ticket.')] 453 | [ValidateSet('1','2','3','6','7','8')] 454 | [Int]$incident_state 455 | ) 456 | 457 | #region Get Headers 458 | Try{ 459 | $Headers = New-WCServiceNowHeaders -Credential $Credential -ErrorAction Stop 460 | } 461 | Catch{ 462 | Write-Error $_ 463 | } 464 | #endregion 465 | 466 | #region Get SysID 467 | Try{ 468 | $SysID = (Get-WCServiceNowIncident -Credential $Credential -IncidentID $IncidentID -ErrorAction Stop).'Sys_ID' 469 | } 470 | Catch{ 471 | Write-Error $_ 472 | } 473 | #endregion 474 | 475 | #region Build URI 476 | $URI = "https://$baseURI/api/now/table/incident/$SysID" 477 | #endregion 478 | 479 | #region Build Body 480 | $Body = @{} 481 | 482 | Foreach($Item in $PSBoundParameters.keys | Where-object {$_ -ne 'Credential' -and $_ -ne 'IncidentID'}){ 483 | $Body.$Item = $PSBoundParameters.Item($Item) 484 | } 485 | 486 | $jsonbody = ConvertTo-Json $Body 487 | 488 | #endregion 489 | 490 | #region Send HTTP request 491 | Try{ 492 | $Response = Invoke-WebRequest -Headers $Headers -Method Put -Uri $URI -Body $JSONBody -ContentType 'application/json' -ErrorAction Stop 493 | $Obj = ($Response.content | convertFrom-JSON).result 494 | Return $Obj 495 | } 496 | Catch{ 497 | Write-error $_ 498 | } 499 | #endregion 500 | } 501 | Function New-WCServiceNowBase64AuthObj{ 502 | [CmdletBinding(SupportsShouldProcess=$False,ConfirmImpact='Low')] 503 | Param( 504 | [Parameter(Mandatory=$True, HelpMessage='This is the API Credential.')] 505 | [ValidateNotNullOrEmpty()] 506 | [PSCredential]$Credential 507 | ) 508 | [String]$textuser = $Credential.UserName 509 | [String]$textPassword = $Credential.GetNetworkCredential().password 510 | $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $TextUser, $Textpassword))) 511 | Return $base64AuthInfo 512 | } 513 | Function New-WCServiceNowHeaders{ 514 | [CmdletBinding(SupportsShouldProcess=$False,ConfirmImpact='Low')] 515 | Param( 516 | [Parameter(Mandatory=$True, HelpMessage='This is the API Credential.')] 517 | [ValidateNotNullOrEmpty()] 518 | [PSCredential]$Credential 519 | ) 520 | Try{ 521 | $base64AuthInfo = New-WCServiceNowBase64AuthObj -Credential $Credential -ErrorAction Stop 522 | } 523 | Catch{ 524 | Write-Error $_ 525 | } 526 | $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" 527 | $headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo)) 528 | $headers.Add('Accept','application/json') 529 | Return $headers 530 | } --------------------------------------------------------------------------------