Using the gc.exe command line application. Start by setting your region, oauth clientid and clientsecret variables. Then get the list of data tables from the API.
$varResponse = .\gc.exe flows datatables list -a --clientid $ClientId --clientsecret $ClientSecret --environment $Region -i --outputformat JSON --profile gc-cli | ConvertFrom-Json
Then loop through the response objects.
$varDataTableJob = .\gc.exe flows datatables export jobs create $($_.id) --clientid $ClientId --clientsecret $ClientSecret --environment $Region --outputformat JSON --profile gc-cli | ConvertFrom-Json
Use a Do{this operation} Until{this condition is met} to query the below API, with a 5 second pause.
$varDataTableStatus = .\gc.exe flows datatables export jobs get $($_.id) $($varDataTableJob.id) --clientid $ClientId --clientsecret $ClientSecret --environment $Region --outputformat JSON --profile gc-cli | ConvertFrom-Json
You can then use the downloadURI from the response to download the file. I set $varDownloadID = $varDataTableStatus.downloadURI
Set your $filepath (save directory + filename) variable.
$varfile = .\gc.exe downloads get $($varDownloadID) --issueRedirect=false --clientid $ClientId --clientsecret $ClientSecret --environment $Region --profile gc-cli | ConvertFrom-Json
I then leveraged a PS System.Net.WebClient operation to download the file with a $varfile.url and $filepath that I set in my script. Keep in mind this WebClient operation can be unreliable with downloading files, giving the script a false-positive response that it saved the file when it didn't actually save. To overcome this problem I placed the WebClient portion into a Do{this} Until{this condition is met} operation. To keep attempting to download the data table until the $filepath is true.
End of loop.
------------------------------
Carlos Albor
Principal PS Consultant
------------------------------