@@ -86,7 +86,11 @@ Function Add-ServiceNowAttachment {
86
86
87
87
[Parameter (Mandatory )]
88
88
[ValidateScript ( {
89
- Test-Path $_
89
+ if ( Test-Path $_ ) {
90
+ $true
91
+ } else {
92
+ throw ' One or more files do not exist'
93
+ }
90
94
})]
91
95
[string []] $File ,
92
96
@@ -105,7 +109,12 @@ Function Add-ServiceNowAttachment {
105
109
[hashtable ] $ServiceNowSession = $script :ServiceNowSession
106
110
)
107
111
108
- begin {}
112
+ begin {
113
+ $auth = Get-ServiceNowAuth - C $Connection - S $ServiceNowSession
114
+ $invokeRestMethodSplat = $auth
115
+ $invokeRestMethodSplat.UseBasicParsing = $true
116
+ $invokeRestMethodSplat.Method = ' POST'
117
+ }
109
118
110
119
process {
111
120
@@ -153,32 +162,34 @@ Function Add-ServiceNowAttachment {
153
162
$thisSysId = $tableRecord.sys_id
154
163
}
155
164
156
- $auth = Get-ServiceNowAuth - C $Connection - S $ServiceNowSession
157
165
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' ) ) {
161
171
# Thanks to https://github.com/samuelneff/MimeTypeMap/blob/master/MimeTypeMap.cs from which
162
172
# 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
164
177
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
+ }
167
182
}
168
183
169
184
# 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 ))) {
179
190
180
- If ($PSCmdlet.ShouldProcess ((' {0} {1}' -f $thisTableName , $thisSysId ), (' Add attachment {0}' -f $FileData.FullName ))) {
181
191
Write-Verbose ($invokeRestMethodSplat | ConvertTo-Json )
192
+
182
193
$response = Invoke-WebRequest @invokeRestMethodSplat
183
194
184
195
if ( $response.Content ) {
0 commit comments