AutomoxAutomox

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

jovann
May 19, 2022 at 9:30 pm UTC
#powershell#policy#file upload

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?

This discussion is archived and closed to replies. Continue it in the Automox Community Slack.

Discuss on Slack

Replies (1)

BenG-AutomoxMay 20, 2022
#1

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!

 

25 lines
$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