|
| 1 | +Function Update-ServiceNowNumber { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Allows for the passing of a number, instead of a sys_id, and associated table to update a ServiceNow entry. |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + Allows for the passing of a number, instead of a sys_id, and associated table to update a ServiceNow entry. Output is suppressed and may be returned with a switch parameter. |
| 8 | +
|
| 9 | + .EXAMPLE |
| 10 | + Update-ServiceNowNumber -Number $Number -Table $Table -Values @{property='value'} |
| 11 | +
|
| 12 | + Updates a ticket number with a value providing no return output. |
| 13 | +
|
| 14 | + .EXAMPLE |
| 15 | + Update-ServiceNowNumber -Number $Number -Table $Table -Values @{property='value'} -PassThru |
| 16 | +
|
| 17 | + Updates a ticket number with a value providing return output. |
| 18 | + .NOTES |
| 19 | +
|
| 20 | + #> |
| 21 | + |
| 22 | + [System.Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingConvertToSecureStringWithPlainText','')] |
| 23 | + [System.Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidGlobalVars','')] |
| 24 | + |
| 25 | + [CmdletBinding(DefaultParameterSetName,SupportsShouldProcess=$true)] |
| 26 | + Param( |
| 27 | + # Object number |
| 28 | + [Parameter(Mandatory=$true)] |
| 29 | + [string]$Number, |
| 30 | + |
| 31 | + # Table containing the entry |
| 32 | + [Parameter(Mandatory=$true)] |
| 33 | + [string]$Table, |
| 34 | + |
| 35 | + # Credential used to authenticate to ServiceNow |
| 36 | + [Parameter(ParameterSetName='SpecifyConnectionFields', Mandatory=$true)] |
| 37 | + [ValidateNotNullOrEmpty()] |
| 38 | + [Alias('ServiceNowCredential')] |
| 39 | + [PSCredential]$Credential, |
| 40 | + |
| 41 | + # The URL for the ServiceNow instance being used |
| 42 | + [Parameter(ParameterSetName='SpecifyConnectionFields', Mandatory=$true)] |
| 43 | + [ValidateNotNullOrEmpty()] |
| 44 | + [string]$ServiceNowURL, |
| 45 | + |
| 46 | + # Azure Automation Connection object containing username, password, and URL for the ServiceNow instance |
| 47 | + [Parameter(ParameterSetName='UseConnectionObject', Mandatory=$true)] |
| 48 | + [ValidateNotNullOrEmpty()] |
| 49 | + [Hashtable]$Connection, |
| 50 | + |
| 51 | + # Hashtable of values to use as the record's properties |
| 52 | + [parameter(Mandatory=$false)] |
| 53 | + [hashtable]$Values, |
| 54 | + |
| 55 | + # Switch to allow the results to be passed back |
| 56 | + [parameter(Mandatory=$false)] |
| 57 | + [switch]$PassThru |
| 58 | + ) |
| 59 | + |
| 60 | + begin {} |
| 61 | + process { |
| 62 | + Try { |
| 63 | + # Use the number and table to determine the sys_id |
| 64 | + $getServiceNowTableEntry = @{ |
| 65 | + Table = $Table |
| 66 | + MatchExact = @{number = $number} |
| 67 | + Credential = $Credential |
| 68 | + ServiceNowURL = $ServiceNowURL |
| 69 | + ErrorAction = 'Stop' |
| 70 | + } |
| 71 | + $SysID = Get-ServiceNowTableEntry @getServiceNowTableEntry | Select-Object -Expand sys_id |
| 72 | + |
| 73 | + # Process credential steps based on parameter set name |
| 74 | + Switch ($PSCmdlet.ParameterSetName) { |
| 75 | + 'SpecifyConnectionFields' { |
| 76 | + $ServiceNowURL = 'https://' + $ServiceNowURL + '/api/now/v1' |
| 77 | + } |
| 78 | + 'UseConnectionObject' { |
| 79 | + $SecurePassword = ConvertTo-SecureString $Connection.Password -AsPlainText -Force |
| 80 | + $Credential = New-Object System.Management.Automation.PSCredential ($Connection.Username, $SecurePassword) |
| 81 | + $ServiceNowURL = 'https://' + $Connection.ServiceNowUri + '/api/now/v1' |
| 82 | + } |
| 83 | + Default { |
| 84 | + If ((Test-ServiceNowAuthIsSet)) { |
| 85 | + $Credential = $Global:ServiceNowCredentials |
| 86 | + $ServiceNowURL = $Global:ServiceNowRESTURL |
| 87 | + } |
| 88 | + Else { |
| 89 | + Throw "Exception: You must do one of the following to authenticate: `n 1. Call the Set-ServiceNowAuth cmdlet `n 2. Pass in an Azure Automation connection object `n 3. Pass in an endpoint and credential" |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + # Convert the values to Json and encode them to an UTF8 array to support special chars |
| 95 | + $Body = $Values | ConvertTo-Json |
| 96 | + $utf8Bytes = [System.Text.Encoding]::Utf8.GetBytes($Body) |
| 97 | + |
| 98 | + # Setup splat |
| 99 | + $Uri = $ServiceNowURL + "/table/$Table/$SysID" |
| 100 | + $invokeRestMethodSplat = @{ |
| 101 | + Uri = $uri |
| 102 | + Method = 'Patch' |
| 103 | + Credential = $Credential |
| 104 | + Body = $utf8Bytes |
| 105 | + ContentType = 'application/json' |
| 106 | + } |
| 107 | + |
| 108 | + If ($PSCmdlet.ShouldProcess("$Table/$SysID",$MyInvocation.MyCommand)) { |
| 109 | + # Send REST call |
| 110 | + $Result = (Invoke-RestMethod @invokeRestMethodSplat).Result |
| 111 | + |
| 112 | + # Option to return results |
| 113 | + If ($PSBoundParameters.ContainsKey('Passthru')) { |
| 114 | + $Result |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + Catch { |
| 119 | + Write-Error $PSItem |
| 120 | + } |
| 121 | + } |
| 122 | + end {} |
| 123 | +} |
0 commit comments