Forum Discussion

Per_Eriksson_37's avatar
Per_Eriksson_37
Icon for Nimbostratus rankNimbostratus
Dec 08, 2018

PowerShell - How to modify system iFile?

I use PowerShell to upload a text file containing a number of parameters that I want to use in an existing iRule.

Through the web-gui I have already created an iFile named 'iFileApiKeys'.

In PowerShell I use this procedure to upload the text file:

$length = "0-" + ($fileContent.Length - 1) + "/" + $fileContent.Length
$headers = @{ "Content-Range" = $length}
$uploadResult = Invoke-WebRequest $URL -Method POST -Headers $headers -InFile $filePathPlusTextfile - 
ContentType "multipart/form-data" -TimeoutSec 20 -Credential $mycreds | ConvertFrom-Json
Write-Host "Upload Result:"
Write-Host $uploadResult

According to the 'uploadResult' the file ends up in the folder:'var/config/rest/downloads/iFileApiKeys.txt' on my F5 LTM

Using 'SuperPutty' I can via tmos (tmsh) modify the existing system iFile by executing:

`tmos> modify  /sys file ifile iFileApiKeys source-path file:///var/config/rest/downloads/iFileApiKeys.txt

My problem is that I can't seem to find the correct PowerShell command to achieve the same result as the tmos (tmsh) command does.

I want to use and actually think I should use:

`Invoke-Webrequest -Method Put`

I've been trying to emulate an example taken from a Jason Rahm post on this site: https://devcentral.f5.com/articles/getting-started-with-icontrol-working-with-the-system-20592

Like this:

$sysIfilePath = "/mgmt/tm/sys/file/ifile/iFileApiKeys"
`$sysPath = "https://" + $host_address + $sysIfilePath
$updateresult = Invoke-WebRequest -Method Put -Uri $sysPath -Headers $headers -Credential $mycreds -Body $body

But this command fails unfortunately,

My assumption is that I don't fill $headers and/or $body with the correct values.

When executing a GET for my sys iFile object the result is:

``{"kind":"tm:sys:file:ifile:ifilestate","name":"iFileApiKeys","fullPath":"iFileApiKeys","generation":10970077,"selfLink":"https://localhost/mgmt/tm/sys/file/ifile/iFileApiKeys?ver=13.1.0.2","chec
ksum":"SHA1:878:52a261b5a113db5c9421a54e1e8b5685e7da7a4d","createTime":"2018-11-26T22:52:08Z","createdBy":"per.eriksson","lastUpdateTime":"2018-12-08T19:49:15Z","mode":33188,"revision":24,"size"
:878,"sourcePath":"file:///var/config/rest/downloads/iFileApiKeys.txt","updatedBy":"per.eriksson"}


Anyone out there that can point me in the right direction on how to update my sys iFile using PowerShell?
Thank you!
/Per   

2 Replies

  • Well, since no one seem to know how to do this I'm posting my own answer to my own question. 🙂

     

    NOTE! ...and this is why I couldn't get it to work:

     

    In order for this to work your service account that you use to log into the F5 LTM need to be administrator level

     

    By combining the Powershell script that uploads the file (example in the initial question above) you can modify a system iFile object using PowerShell like this:

     

    Update the existing system iFile (iFileApiKeys) with uploaded text file content  
    $sysiFileName = "iFileApiKeys"
    $sysIfilePath = "/mgmt/tm/sys/file/ifile/" + $sysiFileName
    $sysPath = "https://" + $host_address + $sysIfilePath
    $sourcePath = "file:///" + "/var/config/rest/downloads/" + $ifileName
    
    $ifleobject = Invoke-WebRequest -Method Get $sysPath -Credential $mycreds
    Write-Host "System iFile object data before update:"
    Write-Host $ifleobject
    
    $headers = @{};
    $headers.Add("ServerHost", $host_address);
     $obj = @{
            sourcePath = $sourcePath
                };
     $body = $obj | ConvertTo-Json  
    
    $updateresult = Invoke-WebRequest -Method Put -Uri $sysPath -Headers $headers -Credential $mycreds -Body $body
    Write-Host "Update Result:"
    Write-Host $updateresult
    
    

    This code will generate this result:

     

    
    
    ...and as you can see the "revision" has incremented by one from 62 to 63.
    Hope it can help someone else!
    /Per