I was building a simple tool to automate some uploads to Azure Blob Storage in Azure Functions. Decided to use PowerShell for that, since well, sounds like a job for PowerShell!
Getting the script to work on my computer was easy, getting it to work on Azure Functions.. not that easy. I wanted to use
File structure from my project:
MyProj +-- MyFunction/ | + function.json | + run.ps1 +-- Modules/ | +-- Azure.Storage/ | +-- 4.6.1/ | + -- (lots of files) +-- host.json +-- requirements.psd1
The modules folder contains the external Azure
Save-Module Azure.Storage -Path .\Modules
In the host.json we enable
{ "version": "2.0", "managedDependency": { "Enabled": "true" } }
requirements.psd1 is simply referencing the Az module:
@{ Az = "1.*" }
I’m using a timer to trigger the function, so function.json is straightforward. Btw, some examples omitted the “name” in the bindings, but omitting this seemed to cause errors.
{ "bindings": [ { "name": "Timer", "type": "timerTrigger", "direction": "in", "schedule": "0 0 7 * * Mon" } ] }
And below is an excerpt from the function itself, run.ps1. I have omitted the actual functionality. The important thing is to include the Enable-AzureRmAlias. This is needed because the automatic dependency management works with the new Az modules, while the old Azure Storage library wants to use the
param($Timer) Enable-AzureRmAlias $context = New-AzureStorageContext -ConnectionString "BlobEndPoint..." Set-AzureStorageBlobContent -Force -Context $context -Container "MyContainer" -File myFile.txt