A question came across the forums a while ago around how to get a human readable version of the configuration on the BIG-IP LTM from the iControl APIs. This question comes up in one form or another every so often so I figured it would be good to write it down with some sample code.
In the forum post, the user wanted an equivalent to the system level "bigpipe list" command from the iControl APIs. Under the seems, the "bigpipe list" command is just an alias for echoing out the contents of the bigip.conf configuration file so really this question boils down to how to download the contents of a file on the system and echo it to the console. In the System.ConfigSync interface, the download_file method will do just that. This example will show how to use the download_file method to download a system configuration file to the local disk and then echo the contents of that file to the console.
The arguments for this application are the address, username, and password of the BIG-IP. This is declared in the top of the script with the following param statement. There is also a Write-Usage function to display the arguments to the user.
As is with all of my PowerShell scripts, the initialization component will look to see if the iControlSnapIn is loaded into the current PowerShell session. If not, the Add-PSSnapIn Cmdlet is called to add the snapin into the runtime. Then a call to the Initialize-F5.iControl cmdlet to setup the connection to the BIG-IP. If configuration succeeds, then the local Download-Configuration function will be called with the specified configuration file (or bigip.conf if one is not).
The local Download-Configuration function will take as a parameter a configuration file name. It then creates some variables for the local file to be downloaded in the current directory and the remote file which resides in the "/config" directory on the BIG-IP. After that, the real fun begins. We start with creating a System.ConfigSync.FileTransferContext object to house the download context and file data. A download chunk size of 64k is used and a local BinaryWriter is created for the local file. The file is actually ascii, but the BinaryWriter will work for both ascii and binary files in case you want to extend this to download other non-ascii files.
Then a loop is entered with consecutive calls to the System.ConfigSync.download_file method. After the method call, the chunked contents are appended to the local file and the loop is repeated until we get a chain type of FILE_LAST OR FILE_FIRST_AND_LAST indicating an end of file occurred. When the loop is existed, the file system objects are closed and control is returned to the calling code. In that code (seen above), the Get-Content PowerShell Cmdlet is called with the local file and the contents of that file are presented to the console.