Skip to main content
Question

Uploadind installation file to a Required Software policy - Powershell -Form Error

  • May 19, 2022
  • 1 reply
  • 226 views

Forum|alt.badge.img

Hello,

I am attempting to create a script that creates/updates a policy and then uploads a file to the policy. So far, so good, but….

While referencing these scripts, in order:
Automox Console API Documentation | Create a New Policy
Automox Console API Documentation | Retrieve a Specific Policy
Automox Console API Docs | Upload Installation File,

and using Powershell (version 5.1.19041.1320) I am bumping into Parameter issues where...the last line of Upload Installation FIle.:

$response = (Invoke-WebRequest -Method Post -Uri $url -Headers $headers -Form $form).Content

Generates this error code: 

Invoke-WebRequest : A paramenter cannot be found that matches parameter name ‘Form’

I’m attempting for use the ‘Try It’ selection, the file uploads with no issue. So I am thinking there’s an issue with the command syntax itself. Any help?

1 reply

Forum|alt.badge.img
  • Automox Employee
  • 8 replies
  • May 20, 2022

Hey @jovann!

Looking at the documentation for Powershell 5 I do not see the Form argument available. I didn’t have Powershell 5 available to me, but you can try an implementation like this instead to work around the missing Form argument during file upload. Let us know how this works for you!

 

$apiKey = 'your api key goes here'

$url = "https://console.automox.com/api/policies/{id}/files"

$FilePath = 'path to your file goes here'

$fileBytes = [System.IO.File]::ReadAllBytes($FilePath)
$fileEnc = [System.Text.Encoding]::GetEncoding('UTF-8').GetString($fileBytes)
$boundary = [System.Guid]::NewGuid().ToString()
$LF = "`r`n"

$headers = @{
    "Authorization" = "Bearer $apiKey"
    "Content-Type" = "multipart/form-data; boundary=`"$boundary`""
}

$bodyLines = (
    "--$boundary",
    "Content-Disposition: form-data; name=`"file`"; filename=`"yourfile.pkg`"",
    "Content-Type: application/octet-stream$LF",
    $fileEnc,
    "--$boundary--$LF"
) -join $LF

$response = (Invoke-WebRequest -Method Post -Uri $url -Headers $headers -Body $bodyLines).Content

 


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings