Forum Discussion
Create a IFile {system level} via API - Powershell
- Aug 24, 2022
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
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
Heres a litte expansion on my full end to end scenario I faced and the solution for some future person to find.
Upload a file with x,y,z contents on a regular basis with a new system level file version (date-time+version) an mapped to the LTM object served to clients
0: Retrieve authentication token
1: Read existing I files in order to obtain & increment the version (not using the F5 incremental but insteaf a static file every time)
2: Create iFile system level object and referance the uploaded file
3: Update existing iRule-> iFile referance the the system level iFile uploaded in step 1/2
#Region Declaration
# Folders & files
$Folder_Path = 'C:\files\'
$File_Upload = "$Folder_Path\MyFile"
$DT_TodaysDate = Get-Date -Format "yyyyMMdd"
$File_Name = 'lan_pac_{date}_v{version}'
$File_Name = $File_Name.Replace('{date}', $DT_TodaysDate)
$File_Name = $File_Name.Replace('{version}', $Version)
$F5LTM_HostIP = 'https://[REDACTED]'
$F5LTM_HostUser = '[REDACTED]'
$F5LTM_HostPass = '[REDACTED]'
#EndRegion
#Region Get API token
$Paramater_InvokeRestMethod = @{
Uri = "{0}{1}" -f $F5LTM_HostIP, '/mgmt/shared/authn/login'
'ContentType' = 'application/json'
body = @{
username = $F5LTM_HostUser
password = $F5LTM_HostPass
loginProviderName = "tmos"
} | ConvertTo-Json -Compress
Method = 'Post'
}
$result = Invoke-RestMethod @Paramater_InvokeRestMethod
${X-F5-Auth-Token} = $result.token.token
#EndRegion
#Region Get list of sys file iFiles & calculate the next version
$Paramater_InvokeRestMethod = @{
Uri = "{0}{1}" -f $F5LTM_HostIP,'/mgmt/tm/sys/file/ifile'
headers = @{
'X-F5-Auth-Token' = ${X-F5-Auth-Token}
}
Method = 'Get'
}
$result = Invoke-RestMethod @Paramater_InvokeRestMethod
#Calculate the next file version
[Int]$Version = $result.items.name | ForEach-Object{
([String]$PSItem).split('v') | Select-Object -Last 1
} | Sort-Object -Descending -Unique | Select-Object -First 1
# Increment version
$Version++
#EndRegion
#Region Upload the next revision file to F5
$File_Name = $File_Name.Replace('{version}', $Version)
# Calculate file length
$Filelength = (Get-Item $File_Upload).length
# Create Overload
$Param_InvokeRestMethod = @{
Uri = "{0}{1}{2}" -f $F5LTM_HostIP, "/mgmt/shared/file-transfer/uploads/", $File_Name
headers = @{
'Content-Type' = 'application/octet-stream'
'X-F5-Auth-Token' = ${X-F5-Auth-Token}
'Content-Range' = "0-$($filelength-1)/$filelength"
}
Method = 'Post'
InFile = $File_Upload
}
try {
$result = Invoke-RestMethod @Param_InvokeRestMethod
} catch [Exception] {
# Can't capture the response code with Invoke-RestMethod, but can catch the expection and assume failure.
Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__
Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
Write-Host $_.Exception
exit
}
#EndRegion
#Region Create a IFile {system level}
$Uri = "{0}{1}" -f $F5LTM_HostIP, "/mgmt/tm/sys/file/ifile/"
# Construct overload definition
$Param_InvokeRestMethod = @{
Uri = $Uri
headers = @{
'Content-Type' = 'application/json'
'X-F5-Auth-Token' = ${X-F5-Auth-Token}
}
body = @{
'name' = $File_Name
'source-path' = "file:/var/config/rest/downloads/$File_Name"
} | ConvertTo-Json -Compress
Method = 'post'
}
# Post & capture result
$result = Invoke-RestMethod @Param_InvokeRestMethod
#EndRegion
#Region Creating an ifile object (LTM level) Not required unless there is no LTM file
$url = "{0}{1}" -f $F5LTM_HostIP, "/mgmt/tm/ltm/ifile/"
$headers = @{
'X-F5-Auth-Token' = ${X-F5-Auth-Token}
'Content-Type' = 'application/json'
}
$body = @{
'name' = $File_Name
'file-name' = $File_Name
} | ConvertTo-Json -Compress
$result = Invoke-RestMethod -Method post -Uri $url -Headers $headers -body $body
#EndRegion
#Region Updating an ifile object (LTM level) required to update the existing iFile LTM object referance to the new file
$Param_InvokeRestMethod = @{
Uri = '{0}{1}' -f $F5LTM_HostIP, '/mgmt/tm/ltm/ifile/client-file'
Headers = @{
'X-F5-Auth-Token' = ${X-F5-Auth-Token}
'Content-Type' = 'application/json'
}
Body = @{
'name' = 'client-file'
'file-name' = $File_Name
} | ConvertTo-Json -Compress
Method = 'Put'
}
$result = Invoke-RestMethod @Param_InvokeRestMethod
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com