jhorsman
9/6/2019 - 8:25 AM

Set-DxAddon.ps1 Upload SDL Tridion Sites 9.1 (and up) add-ons to the Add-on service

# Usage examples
#   .\Set-DxAddon.ps1 -Url "http://server:83" -Path addon.zip
#   .\Set-DxAddon.ps1 -Path addon.zip

param (
    [parameter(Mandatory=$false, HelpMessage="Tridion DX Add-on service URL. Defaults to 'http://localhost:83'")]
    [string] $Url = "http://localhost:83",

    [parameter(Mandatory=$true, HelpMessage="Path to to the add-on.")]
    [string] $Path
)

$ErrorActionPreference = "Stop"

if(-not (Test-Path $Path))
{
    Write-Error "File '$Path' does not exist"
}

$filename = Split-Path $Path -Leaf
$fileBytes = [System.IO.File]::ReadAllBytes($Path);
$fileEnc = [System.Text.Encoding]::GetEncoding('ISO-8859-1').GetString($fileBytes);
$boundary = [System.Guid]::NewGuid().ToString(); 
$LF = "`r`n";
$bodyLines = ( 
    "--$boundary",
    "Content-Disposition: form-data; name=`"File`"; filename=`"$filename`"",
    "Content-Type: application/x-zip-compressed$LF",
    $fileEnc,
    "--$boundary--$LF" 
) -join $LF

$postUrl = $Url + "/addon/api/v1/addons"
Invoke-RestMethod -Uri $postUrl -Method Post -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $bodyLines