|
| 1 | +Function Add-ServiceNowAttachment { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Attaches a file to an existing ticket. |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + Attaches a file to an existing ticket. |
| 8 | +
|
| 9 | + .PARAMETER Number |
| 10 | + ServiceNow ticket number |
| 11 | +
|
| 12 | + .PARAMETER Table |
| 13 | + ServiceNow ticket table name |
| 14 | +
|
| 15 | + .PARAMETER File |
| 16 | + A valid path to the file to attach |
| 17 | +
|
| 18 | + .EXAMPLE |
| 19 | + Add-ServiceNowAttachment -Number $Number -Table $Table -File .\File01.txt, .\File02.txt |
| 20 | +
|
| 21 | + Upload one or more files to a ServiceNow ticket by specifing the number and table |
| 22 | +
|
| 23 | + .EXAMPLE |
| 24 | + Add-ServiceNowAttachment -Number $Number -Table $Table -File .\File01.txt -ContentType 'text/plain' |
| 25 | +
|
| 26 | + Upload a file and specify the MIME type (content type). Should only be required if the function cannot automatically determine the type. |
| 27 | +
|
| 28 | + .EXAMPLE |
| 29 | + Add-ServiceNowAttachment -Number $Number -Table $Table -File .\File01.txt -PassThru |
| 30 | +
|
| 31 | + Upload a file and receive back the file details. |
| 32 | +
|
| 33 | + .OUTPUTS |
| 34 | + System.Management.Automation.PSCustomObject |
| 35 | +
|
| 36 | + .NOTES |
| 37 | +
|
| 38 | + #> |
| 39 | + |
| 40 | + [System.Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingConvertToSecureStringWithPlainText','')] |
| 41 | + [System.Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidGlobalVars','')] |
| 42 | + |
| 43 | + [OutputType([PSCustomObject[]])] |
| 44 | + [CmdletBinding(DefaultParameterSetName,SupportsShouldProcess=$true)] |
| 45 | + Param( |
| 46 | + # Object number |
| 47 | + [Parameter(Mandatory=$true)] |
| 48 | + [string]$Number, |
| 49 | + |
| 50 | + # Table containing the entry |
| 51 | + [Parameter(Mandatory=$true)] |
| 52 | + [string]$Table, |
| 53 | + |
| 54 | + # Filter results by file name |
| 55 | + [parameter(Mandatory=$true)] |
| 56 | + [ValidateScript({ |
| 57 | + Test-Path $_ |
| 58 | + })] |
| 59 | + [string[]]$File, |
| 60 | + |
| 61 | + # Content (MIME) type - if not automatically determined |
| 62 | + [Parameter(Mandatory=$false)] |
| 63 | + [string]$ContentType, |
| 64 | + |
| 65 | + # Credential used to authenticate to ServiceNow |
| 66 | + [Parameter(ParameterSetName='SpecifyConnectionFields', Mandatory=$true)] |
| 67 | + [ValidateNotNullOrEmpty()] |
| 68 | + [Alias('ServiceNowCredential')] |
| 69 | + [PSCredential]$Credential, |
| 70 | + |
| 71 | + # The URL for the ServiceNow instance being used |
| 72 | + [Parameter(ParameterSetName='SpecifyConnectionFields', Mandatory=$true)] |
| 73 | + [ValidateScript({Test-ServiceNowURL -Url $_})] |
| 74 | + [ValidateNotNullOrEmpty()] |
| 75 | + [Alias('Url')] |
| 76 | + [string]$ServiceNowURL, |
| 77 | + |
| 78 | + # Azure Automation Connection object containing username, password, and URL for the ServiceNow instance |
| 79 | + [Parameter(ParameterSetName='UseConnectionObject', Mandatory=$true)] |
| 80 | + [ValidateNotNullOrEmpty()] |
| 81 | + [Hashtable]$Connection, |
| 82 | + |
| 83 | + # Allow the results to be shown |
| 84 | + [Parameter()] |
| 85 | + [switch]$PassThru |
| 86 | + ) |
| 87 | + |
| 88 | + begin {} |
| 89 | + process { |
| 90 | + Try { |
| 91 | + # Use the number and table to determine the sys_id |
| 92 | + $getServiceNowTableEntry = @{ |
| 93 | + Table = $Table |
| 94 | + MatchExact = @{number = $number} |
| 95 | + ErrorAction = 'Stop' |
| 96 | + } |
| 97 | + |
| 98 | + # Update the Table Splat if an applicable parameter set name is in use |
| 99 | + Switch ($PSCmdlet.ParameterSetName) { |
| 100 | + 'SpecifyConnectionFields' { |
| 101 | + $getServiceNowTableEntry.Add('Credential', $Credential) |
| 102 | + $getServiceNowTableEntry.Add('ServiceNowURL', $ServiceNowURL) |
| 103 | + } |
| 104 | + 'UseConnectionObject' { |
| 105 | + $getServiceNowTableEntry.Add('Connection', $Connection) |
| 106 | + } |
| 107 | + Default { |
| 108 | + If (-not (Test-ServiceNowAuthIsSet)) { |
| 109 | + 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" |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + $TableSysID = Get-ServiceNowTableEntry @getServiceNowTableEntry | Select-Object -Expand sys_id |
| 115 | + |
| 116 | + # Process credential steps based on parameter set name |
| 117 | + Switch ($PSCmdlet.ParameterSetName) { |
| 118 | + 'SpecifyConnectionFields' { |
| 119 | + $ApiUrl = 'https://' + $ServiceNowURL + '/api/now/v1/attachment' |
| 120 | + } |
| 121 | + 'UseConnectionObject' { |
| 122 | + $SecurePassword = ConvertTo-SecureString $Connection.Password -AsPlainText -Force |
| 123 | + $Credential = New-Object System.Management.Automation.PSCredential ($Connection.Username, $SecurePassword) |
| 124 | + $ApiUrl = 'https://' + $Connection.ServiceNowUri + '/api/now/v1/attachment' |
| 125 | + } |
| 126 | + Default { |
| 127 | + If ((Test-ServiceNowAuthIsSet)) { |
| 128 | + $Credential = $Global:ServiceNowCredentials |
| 129 | + $ApiUrl = $Global:ServiceNowRESTURL + '/attachment' |
| 130 | + } |
| 131 | + Else { |
| 132 | + 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" |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + ForEach ($Object in $File) { |
| 138 | + $FileData = Get-ChildItem $Object -ErrorAction Stop |
| 139 | + If (-not $ContentType) { |
| 140 | + Add-Type -AssemblyName 'System.Web' |
| 141 | + $ContentType = [System.Web.MimeMapping]::GetMimeMapping($FileData.FullName) |
| 142 | + } |
| 143 | + |
| 144 | + # POST: https://instance.service-now.com/api/now/attachment/file?table_name=incident&table_sys_id=d71f7935c0a8016700802b64c67c11c6&file_name=Issue_screenshot |
| 145 | + $Uri = "{0}/file?table_name={1}&table_sys_id={2}&file_name={3}" -f $ApiUrl,$Table,$TableSysID,$FileData.Name |
| 146 | + |
| 147 | + $invokeRestMethodSplat = @{ |
| 148 | + Uri = $Uri |
| 149 | + Headers = @{'Content-Type' = $ContentType} |
| 150 | + Method = 'POST' |
| 151 | + InFile = $FileData.FullName |
| 152 | + Credential = $Credential |
| 153 | + } |
| 154 | + |
| 155 | + If ($PSCmdlet.ShouldProcess($Uri,$MyInvocation.MyCommand)) { |
| 156 | + $Result = (Invoke-RestMethod @invokeRestMethodSplat).Result |
| 157 | + |
| 158 | + If ($PassThru) { |
| 159 | + $Result | Update-ServiceNowDateTimeField |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + Catch { |
| 165 | + Write-Error $PSItem |
| 166 | + } |
| 167 | + } |
| 168 | + end {} |
| 169 | +} |
0 commit comments