Demystifying iControl REST Part 5: Transferring Files
Updated Aug 04, 2023
Version 5.0Was this article helpful?
In case anyone else struggled with this in powershell (I sure did for a bit). Here is an simple function that will do the trick.
function F5-RestFileUpload
{
<
.SYNOPSIS
Upload a file to the BigIP
.DESCRIPTION
Uploads file to the F5 in to /var/config/rest/downloads directory.
.EXAMPLE
F5-RestFileUpload -InFile "C:\certs\mycert.pxf" -BigIP "mybigip.local" -Credential $mycreds
>
[CmdletBinding()]
PARAM
(
[string][parameter(Mandatory = $true)][ValidateNotNullOrEmpty()]$InFile,
[string][parameter(Mandatory = $true)][ValidateNotNullOrEmpty()]$BigIP,
[System.Management.Automation.PSCredential]$Credential
)
BEGIN
{
$file = Get-ChildItem $InFile
$byte = Get-Content $file.FullName -Encoding Byte
$ContentType = "application/octet-stream"
$ContentRange = "0-{0}/{1}" -f ($file.length -1), $file.length
$path = "/mgmt/shared/file-transfer/uploads"
$uri = "https://{0}{1}/{2}" -f $BigIP, $path, $file.name
$uri = New-Object System.Uri $uri
}
PROCESS
{
$webclient = New-Object System.Net.WebClient
$webclient.Headers.Add("ServerHost", $uri.DnsSafeName);
$webclient.Headers.Add("Content-Type", $ContentType)
$webclient.Headers.Add("Content-Range", $ContentRange)
$webclient.Credentials = $Credential
try
{
$response = $webclient.uploaddata($uri.AbsoluteUri, $byte)
}
catch [Exception]
{
$PSCmdlet.ThrowTerminatingError($_)
}
finally
{
if($null -ne $webclient)
{
$webclient.Dispose()
}
}
}
END { }
}