@@ -86,7 +86,11 @@ Function Add-ServiceNowAttachment {
8686
8787 [Parameter (Mandatory )]
8888 [ValidateScript ( {
89- Test-Path $_
89+ if ( Test-Path $_ ) {
90+ $true
91+ } else {
92+ throw ' One or more files do not exist'
93+ }
9094 })]
9195 [string []] $File ,
9296
@@ -105,7 +109,12 @@ Function Add-ServiceNowAttachment {
105109 [hashtable ] $ServiceNowSession = $script :ServiceNowSession
106110 )
107111
108- begin {}
112+ begin {
113+ $auth = Get-ServiceNowAuth - C $Connection - S $ServiceNowSession
114+ $invokeRestMethodSplat = $auth
115+ $invokeRestMethodSplat.UseBasicParsing = $true
116+ $invokeRestMethodSplat.Method = ' POST'
117+ }
109118
110119 process {
111120
@@ -153,32 +162,34 @@ Function Add-ServiceNowAttachment {
153162 $thisSysId = $tableRecord.sys_id
154163 }
155164
156- $auth = Get-ServiceNowAuth - C $Connection - S $ServiceNowSession
157165
158- ForEach ($Object in $File ) {
159- $FileData = Get-ChildItem $Object - ErrorAction Stop
160- If (-not $ContentType ) {
166+ foreach ($thisFile in $File ) {
167+
168+ $thisFileObject = Get-ChildItem $thisFile
169+
170+ If ( -not $PSBoundParameters.ContainsKey (' ContentType' ) ) {
161171 # Thanks to https://github.com/samuelneff/MimeTypeMap/blob/master/MimeTypeMap.cs from which
162172 # MimeTypeMap.json was adapted
163- $ContentTypeHash = ConvertFrom-Json (Get-Content " $PSScriptRoot \..\config\MimeTypeMap.json" - Raw)
173+ $contentTypes = ConvertFrom-Json (Get-Content " $PSScriptRoot \..\config\MimeTypeMap.json" - Raw)
174+
175+ $Extension = [IO.Path ]::GetExtension($thisFileObject.FullName )
176+ $ContentType = $contentTypes .$Extension
164177
165- $Extension = [IO.Path ]::GetExtension($FileData.FullName )
166- $ContentType = $ContentTypeHash .$Extension
178+ if ( -not $ContentType ) {
179+ Write-Error (' Content type not found for {0}, the file will not be uploaded' -f $thisFileObject.FullName )
180+ Continue
181+ }
167182 }
168183
169184 # POST: https://instance.service-now.com/api/now/attachment/file?table_name=incident&table_sys_id=d71f7935c0a8016700802b64c67c11c6&file_name=Issue_screenshot
170- # $Uri = "{0}/file?table_name={1}&table_sys_id={2}&file_name={3}" -f $ApiUrl, $Table, $TableSysID, $FileData.Name
171- $invokeRestMethodSplat = $auth
172- $invokeRestMethodSplat.Uri += ' /attachment/file?table_name={0}&table_sys_id={1}&file_name={2}' -f $thisTableName , $thisSysId , $FileData.Name
173- $invokeRestMethodSplat.Headers += @ {' Content-Type' = $ContentType }
174- $invokeRestMethodSplat.UseBasicParsing = $true
175- $invokeRestMethodSplat += @ {
176- Method = ' POST'
177- InFile = $FileData.FullName
178- }
185+ $invokeRestMethodSplat.Uri = ' {0}/attachment/file?table_name={1}&table_sys_id={2}&file_name={3}' -f $auth.Uri , $thisTableName , $thisSysId , $thisFileObject.Name
186+ $invokeRestMethodSplat.ContentType = $ContentType
187+ $invokeRestMethodSplat.InFile = $thisFileObject.FullName
188+
189+ If ($PSCmdlet.ShouldProcess ((' {0} {1}' -f $thisTableName , $thisSysId ), (' Add attachment {0}' -f $thisFileObject.FullName ))) {
179190
180- If ($PSCmdlet.ShouldProcess ((' {0} {1}' -f $thisTableName , $thisSysId ), (' Add attachment {0}' -f $FileData.FullName ))) {
181191 Write-Verbose ($invokeRestMethodSplat | ConvertTo-Json )
192+
182193 $response = Invoke-WebRequest @invokeRestMethodSplat
183194
184195 if ( $response.Content ) {
0 commit comments