Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load a file in Azure Function using PowerShell

I have a small function that takes an array of US telephone area codes and returns an array of area codes paired to the state they are in.

The "master list" of area codes to states is a long variable in my function. I'd like to put that in a CSV file instead.

I'm a beginner to using Azure Functions but, even when the file is in the root folder, Get-Content .\filename.csv does not work.

like image 706
Chris76786777 Avatar asked Sep 13 '25 07:09

Chris76786777


2 Answers

It's never really a good idea to reference absolute paths as things may change. To use a variable that will always point you to the correct path you can use:

$EXECUTION_CONTEXT_FUNCTIONDIRECTORY which will return the current folder your Azure Functions runs from.

To use this in your question at hand:

Get-Content -Path "$EXECUTION_CONTEXT_FUNCTIONDIRECTORY\filename.csv"

Documentation on this: https://github.com/Azure/azure-functions-host/wiki/Retrieving-information-about-the-currently-running-function#powershell-php-python-bash-batch-and-other-scripting-languages

like image 130
Koen Zomers Avatar answered Sep 14 '25 21:09

Koen Zomers


The path to your Azure Function directory is

D:\home\site\wwwroot\<YourFunctionName>

To load the CSV file, use the following in your PowerShell script,

Get-Content D:\home\site\wwwroot\<YourFunctionName>\filename.csv

Here's a sample output for loading the areacode-to-state mapping file,

2017-03-11T09:21:57.185 Function started (Id=9c93e8cf-4cf0-487e-b86e-02e57b41b8de)
2017-03-11T09:21:58.357 Area code,State,State code
2017-03-11T09:21:58.357 201,New Jersey,NJ
2017-03-11T09:21:58.357 202,"Washington,DC",DC
2017-03-11T09:21:58.357 203,Connecticut,CT
2017-03-11T09:21:58.357 205,Alabama,AL
....<more logging>....


Additional tooling and resources:

i. Kudu console - You may use the Kudu console to navigate through the directory structure of your Function App. I sometimes use the Kudu console to test the run.ps1 script outside the Azure Functions runtime.

To use the Kudu console, perform the following steps:

  1. Visit the Azure Functions Portal for your Function App.
  2. Click on Function app settings -> Go to Kudu -> Debug Console -> PowerShell.
  3. Navigate to your Function directory and run your script as shown in the snapshot below.

    enter image description here


ii. Bring your own modules - You may also load your own custom modules and use them in your run.ps1 script. Details on how to achieve this are provide here.

like image 29
Ling Toh Avatar answered Sep 14 '25 22:09

Ling Toh