PowerShell Example: Upload Attachments
The following example shows how to upload attachments using PowerShell.
# Set server login variables $serverName = "your server" $apiKey = "your client id" $userName = "CSDAdmin" $password = "CSDAdmin" $baseUri = "http://${serverName}/CherwellAPI/" # Get an access token $tokenUri = $baseUri + "token" $authMode = "Internal" $tokenRequestBody = @{ "Accept" = "application/json"; "grant_type" = "password"; "client_id" = $apiKey; "username" = $userName; "password"= $password } $tokenResponse = Invoke-RestMethod -Method POST -Uri "${tokenUri}?auth_mode=${authMode}&api_key=${apiKey}" -Body $tokenRequestBody $requestHeader = @{ Authorization = "Bearer $($tokenResponse.access_token)" } # Get the business object summary for Incident. This will give us the busObId $summaryUri = $baseUri + "api/V1/getbusinessobjectsummary/busobname/Incident" $summaryResults = Invoke-RestMethod -Method GET -Uri $summaryUri -ContentType application/json -Header $requestHeader $busObId = $summaryResults[0].busobId # Get an incident rec id from the public id $getIncidentUri = $baseUri + "api/V1/getbusinessobject/busobid/${busobid}/publicid/102521" $getIncidentResponse = Invoke-RestMethod -Method GET -Uri $getIncidentUri -ContentType application/json -Header $requestHeader $busObRecId = $getIncidentResponse.busObRecId # Create a text file "This file was attached to this incident by the API example for PowerShell" | Out-File -FilePath "$($env:TEMP)\test.txt" $file = Get-ChildItem -Path "$($env:TEMP)\test.txt" $filename = $file.Name $totalsize = $file.Length $allFileBytes = [System.IO.File]::ReadAllBytes($file.FullName) $offset = 0 # Upload the file to the incident $uploadAttachmentUri = $baseUri + "api/V1/uploadbusinessobjectattachment/filename/${filename} /busobid/${busobid}/busobrecid/${busobrecid}/offset/${offset}/totalsize/${totalsize}" $uploadAttachmentRequest = $allFileBytes $uploadAttachmentResponse = Invoke-RestMethod -Method POST -Uri $uploadAttachmentUri -ContentType application/json -Header $requestHeader -Body $uploadAttachmentRequest # Delete the text file that was created Remove-Item $file.FullName -Force