Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How can I load custom modules in an Azure Functions PowerShell script


How can I load custom modules in an Azure Functions PowerShell script


Loading custom PowerShell modules in Azure Functions involves a few steps. Here's how you can do it:

Using the Modules Folder

1. Create a Modules Folder: At the root of your Azure Function App, create a folder named Modules. This can be done using the App Service Editor in the Azure portal.

2. Upload Custom Modules: Place your custom module files (e.g., `.psm1` and `.psd1`) inside this Modules folder. You can upload these files using the App Service Editor or by FTP.

3. Restart the Function App: After uploading the modules, restart your Azure Function App to ensure the modules are loaded and available for use.

4. Import Modules in Your Script: You don't need to explicitly import modules in your PowerShell script if they are in the Modules folder, as Azure Functions automatically adds this folder to the `$env:PSModulePath`. However, if you need to import a module explicitly for any reason, you can use the `Import-Module` cmdlet.

Using Kudu Console

Alternatively, you can use the Kudu console to upload modules:

1. Access Kudu Console: Go to your Azure Function App in the Azure portal, and navigate to Advanced Tools (Kudu).

2. Upload Modules: In the Kudu console, navigate to the site/wwwroot directory of your Function App. Create a bin folder if needed, and upload your custom module files there.

3. Import Modules in Your Script: Since the modules are not in a default path, you need to import them explicitly using the `Import-Module` cmdlet with the full path to the module's `.psd1` file.

Important Considerations

- Module Path: Ensure that the modules are placed in a location that is included in the `$env:PSModulePath`, or import them explicitly.
- Restart: After adding or updating modules, restart your Function App to apply changes.
- Version Compatibility: Ensure that your custom modules are compatible with the PowerShell version used by Azure Functions.

By following these steps, you can successfully load and use custom PowerShell modules in your Azure Functions scripts.

Citations:
[1] https://jeffbrown.tech/azure-functions-powershell-module/
[2] https://blog.tyang.org/2016/10/07/using-custom-powershell-modules-in-azure-functions/
[3] https://4bes.nl/2021/07/11/add-custom-modules-to-azure-functions-for-powershell/
[4] https://stackoverflow.com/questions/72916606/custom-powershell-module-fails-to-import-in-azure-funtion
[5] https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-powershell
[6] https://devblogs.microsoft.com/powershell/using-powershell-modules-in-azure-functions/
[7] https://tech.nicolonsky.ch/azure-functions-powershell-modules/
[8] https://pnp.github.io/powershell/articles/azurefunctions.html