2
votes

I'm working with PowerShell and Azure functions, both which are new to me, but it's choices I've been tasked to work with.

What I reeeally want to do is this:

Invoke-RestMethod -Method GET -Uri $uri -Headers $headers -Body $body -OutFile $outfile -UseBasicParsing -DisableKeepAlive

Temporarily write the REST response to $outfile and then a moment later in the script I'll upload the file via FTP to somewhere else and then won't need it anymore.

Of course, this works great locally. But in Azure I essentially get that the file cannot be found at the $outfile path location. It shouldn't be trying to find it there anyway, but I can only guess I don't actually have permissions to write to that file system.

So if you know of a way for me to write a temp file here, that would be preferred!

Incase not, some blob storage was generated with the function app and I've got an environment variable of AzureWebJobsStorage with the account name, key, etc, for that storage. So maybe I can write the files to there, then read them back.

But, I can't find any documentation anywhere of how to do this in PowerShell. There's a lot of talk out there of the AzureWebJobsStorage variable, but I didn't find anywhere where it was actually used with an example of how to read and write in PowerShell. Again, because I'm just very new to the language.

Any assistance would be greatly appreciated. Thanks!

1

1 Answers

3
votes

The location returned by the New-TemporaryFile cmdlet is writable, so you can do this:

$tempFile = New-TemporaryFile
Invoke-RestMethod -Method GET -Uri $uri -Headers $headers -Body $body -OutFile $tempFile -UseBasicParsing -DisableKeepAlive
...
Remove-Item $tempFile

Please note that this temporary is guaranteed to be available within the context of the same function invocation only. Other function invocations may be dispatched to other workers, and this file will not be available anymore.

If you do want to share the file between invocations, you can store it under D:\home\data, but then be aware that this location is shared between workers, so you may need to handle potential concurrent access etc. You can also access the storage account directly by using Az.Storage cmdlets. The AzureWebJobsStorage variable contains a connection string that can be passed to the New-AzStorageContext, and then you can store your data in a blob (you will find the examples at https://docs.microsoft.com/en-us/powershell/module/az.storage).