Add a custom or non-standard PowerShell module to Azure Functions

When you need to do a bit more than an Azure Logic App will let you or when there is a bit of custom code/actions that need to be taken this where do you go? That's right, Azure Functions! Azure Functions allow quite a bit of flexibility and customization including a bunch of languages we can write in! PowerShell is one of those (In beta as of this writing) however, it seems the question of the day here lately is; How do I add my own custom module to my PowerShell Azure Function???!!! Well it is really quite simple and that is the purpose of this blog post so let's get to it.

First off you need your module, now I have created my own but you can reference and use any module in your Azure Functions app. The first detail of info to share here is that this is NOT application wide, you will need to do this for each function inside your app that you want to have access to this external resource.

Step 1: Creating the folder to store it in, in the right place. You need to have a folder named "modules" in the root folder of your function, this is the folder where the run.ps1 is located. How do we do that? Kudu is the answer.

  • Go to your Functions App -> Click on the Platform Features tab -> Under Development Tools choose Advanced Tools (Kudu) :

  • Next, On the Kudu page at the top, Click on the Debug Console drop down and choose PowerShell or Cmd (doesn't matter). Both will lead you to a view of your folder structure and a console window.

 

  • Now we need to navigate to your Functions Site Directory. My function is named PowerShellTimed (as you see in the screen shot above) so that is where I will navigate too. Click on the folder Site -> wwwroot -> YourFunctionsName and you will land a folder that has your run.ps1 and a function.json like below:

  • Now you are going to click on the + sign and choose New Folder and you will name it "modules":

 

  • Ok, we have our folder created and now we can journey back to the Azure Portal. Navigate to your Functions App and click on the PowerShell function you are working with. All the way on the right of the screen (on the side) you should see a tab for "View files" and click on it. That will open your files blade and you should see a folder "modules" and your function.json and run.ps1.

  • Now, click on the modules folder and click Upload, this will open a folder browser dialog. Navigate to your files and put them directly in the modules folder.

NOTE: Do NOT create any sub folders and place them in there, the files must be in the root modules folder even if you have many modules you are wanting to place here.

  • Once that is done, simply call your module commands from the run.ps1 script and viola it works! You do NOT need to call import-modules UNLESS you have conflicting modules and you want to explicitly override them. Notice I have two command I am calling directly Get-FooDate and Write-Foo and as you can see in the logs below work successfully!

 

 

Hope that helps my friends, enjoy and PowerShell \ Azure on!

~Theo