Create a IFile {system level} via API - Powershell
Hi All,
Attempting the following:
1: Create iFile system level
2: Update existing iRule-> iFile referance the the file uploaded in step 1.
Im getting stuck at step 1, any assistance creatly apprecuated.
What I have tried:
Get Auth Token:
# Get API token
$big_ip = 'https://[REDACTED]'
$url = "{0}{1}" -f $big_ip, '/mgmt/shared/authn/login'
$body = @{
username = "[REDACTED]"
password = '[REDACTED]'
loginProviderName = "tmos"
} | ConvertTo-Json
$result = Invoke-RestMethod -Method 'POST' -Uri $url -ContentType 'application/json' -Body $body
$token = $($result.token.token)
Works.
Next, Upload my file:
$File_Name = 'MyFile'
$File_Upload = 'C:\Temp\MyFile'
$url = "{0}{1}{2}" -f $big_ip, "/mgmt/shared/file-transfer/uploads/", $File_Name
$filelength = (Get-Item $File_Upload).length
$headers = @{
'Content-Type' = 'application/octet-stream'
'X-F5-Auth-Token' = $token
'Content-Range' = "0-$($filelength-1)/$filelength"
}
$result = Invoke-RestMethod -Method Post -Uri $url -Headers $headers -InFile $File_Upload
Works fine too I am returned with:
remainingByteCount : 0
usedChunks : @{0=46321}
totalByteCount : 46321
localFilePath : /var/config/rest/downloads/MyFile
temporaryFilePath : /var/config/rest/downloads/tmp/MyFile
generation : 0
lastUpdateMicros : 1661257236246203
Next is where im stuck, creation of the iFile system level from the uploaded file.
Ive re-typed the below from the Curl samples here: Syncing local repositories and ifiles using iContr... - DevCentral (f5.com)
### Create a iFile {system level} - does not yet work
$File_Name = 'MyFile'
$url = "{0}{1}{2}" -f $big_ip, "/mgmt/tm/sys/file/ifile/", $File_Name
$headers = @{
'Content-Type' = 'application/json'
'X-F5-Auth-Token' = $token
}
$body = @{
'name' = $File_Name
'source-path' = "file:///var/config/rest/downloads/$File_Name"
} | ConvertTo-Json
$result = Invoke-RestMethod -Method put -Uri $url -Headers $headers -Body $body
Next I am recieving that the file cannot be found. So this query is incorrectly tructured ?
Translated these examples to powershell:
Syncing local repositories and ifiles using iContr... - DevCentral (f5.com)
Invoke-RestMethod : {"code":404,"message":"01020036:3: The requested iFile (/Common/MyFile) was not found.","errorStack":[],"apiError":3}
At line:1 char:11
+ $result = Invoke-RestMethod -Method put -Uri $url -Headers $headers - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
Any assistance appreated with the above & next up updating the iRule file refence.
Found my fault... JRahm & Patrik_Jonsson this was after my long reply to your other post JRahm (and yes it works on a POST method)... I had the file name in the URL overload specified on the initial create request, it should not be in there...
so this..$File_Name = 'MyFile' $url = "{0}{1}{2}" -f $big_ip, "/mgmt/tm/sys/file/ifile/",$File_Name $headers = @{ 'Content-Type' = 'application/json' 'X-F5-Auth-Token' = $token } $body = @{ 'name' = $File_Name 'source-path' = "file:/var/config/rest/downloads/$File_Name" } | ConvertTo-Json -Compress $result = Invoke-RestMethod -Method post -Uri $url -Headers $headers -Body $body
needs to be:$File_Name = 'MyFile' $url = "{0}{1}" -f $big_ip, "/mgmt/tm/sys/file/ifile/" $headers = @{ 'Content-Type' = 'application/json' 'X-F5-Auth-Token' = $token } $body = @{ 'name' = $File_Name 'source-path' = "file:/var/config/rest/downloads/$File_Name" } | ConvertTo-Json -Compress $result = Invoke-RestMethod -Method post -Uri $url -Headers $headers -Body $body
without the file name in the initial overload.
Hope this helps some future person 🙂
Off to the next challange.
Bonus: Retyped to splat (overload definition) - because why not.# Create a IFile {system level} $File_Name = 'MyFile' $Uri = "{0}{1}" -f $big_ip, "/mgmt/tm/sys/file/ifile/" # Construct overload definition $Param_InvokeRestMethod = @{ headers = @{ 'Content-Type' = 'application/json' 'X-F5-Auth-Token' = $token } body = @{ 'name' = $File_Name 'source-path' = "file:/var/config/rest/downloads/$File_Name" } | ConvertTo-Json -Compress Method = 'post' Uri = $Uri } # Post & capture result $result = Invoke-RestMethod @Param_InvokeRestMethod