Azure Functions app and custom PowerShell modules

Azure Functions is a service that allows developers to write code without worrying about application infrastructure. There are two major benefits of using the Azure Functions app:

  • Since the underlying infrastructure is provided by Microsoft as part of the Azure Functions app service, developers just need to design the code without having to explicitly provision or manage the underlying infrastructure.
  • You can run your code as a PowerShell script and deploy it in the scheduled tasks, but you still have a dependency on the virtual machine or where the scheduled task will run.

Apart from running C#, Java, and other supported languages, the Azure Functions app service also supports running PowerShell cmdlets. You can build your PowerShell script and have it run as part of the Azure Functions app. However, there is one challenge for developers to run the desired code. For example, if you have designed a PowerShell script that uses custom modules designed in-house, the Azure Functions app cannot import custom modules automatically and install it for you.

There are few things to consider when using Azure Functions app for PowerShell:

Default Functions PowerShell script

When you create an Azure Functions app, it creates a default PowerShell script named “Run.PS1” and a JSON file named Function.JSON file. The “Run.PS1” is where your code resides, and it is executed when trigger takes place or when someone invokes REST API using REST API calls or using “Invoke-RestMethod” PowerShell cmdlet. The default “Run.PS1” resides in the working directory which is D:\Home\Site\wwwroot\{FunctionName}.

Default directory for Functions

You can only work with the directory to which you have rights. When you create an Azure Functions using Azure Functions app service, the process creates a directory under D:\Home\Site\wwwroot\{FunctionName}. As you can see in the screenshot below, I created a function named HttpTriggerPowerShell and Azure Functions app created a folder for me to work in.

All your files such as Run.PS1 and Function.JSON files reside under {functionname} directory as shown in the screenshot below. You can also create custom files if you need to. As you can see in the screenshot below, I created a file by name “ss.txt” by using “Echo Test > SS.txt” command.

Other than D:\Home\Site\wwwroot\{Functionname} directory, you also have access to D:\Local\Temp directory. This directory is used by the Azure Functions app to store temporary files during the execution process.

Default PowerShell modules

By default, Azure Functions app ships with default PowerShell modules for Azure services including Azure Resource Manager Modules, Azure Site Recovery, and Azure Websites and default Windows PowerShell modules such as App Locker, Appx, and BitsTransfer. If you need to see the PowerShell modules that ship with Azure Functions App, simply execute the “PowerShell.exe Get-Module -ListAvailable” command under the “Console” tab as shown in the screenshot below:

No PowerShell console

Note that the Azure Functions app does not provide a PowerShell console window that could be used to execute PowerShell commands directly as you do on your desktop computer. The “Run.PS1” is always executed by PowerShell.exe, which is available as part of the function. In case you need to execute a PowerShell command, you need to use PowerShell.exe. For example, to execute a PowerShell command to list PowerShell modules, you will execute PowerShell.exe Get-Module -ListAvailable. Similarly, if you would like to execute any other PowerShell command that is already loaded as part of the default modules, you will use “PowerShell.exe” before the command.

Cannot install PowerShell modules by using install-module

Note that using “Install-Module” you cannot install PowerShell modules as you do on your desktop computer. For example, if you try to install a PowerShell module that is not available as part of the default PowerShell modules list, you will get this error message:

As you can see in the screenshot above, I am trying to install PowerShell modules for Azure Windows Virtual Desktop, but the command returns an error saying “Administrator rights are required to install modules”. This is because by default Install-Module targets PowerShell environment directory to install to the modules and as stated above you do not have access to PowerShell environment directory other than D:\Home\site\wwwroot\{functionname} and D:\Local\Temp directories. Even if you try to install PowerShell modules or save packages as part of “Run.PS1”, you will get a similar message as shown in the screenshot above.

Adding custom modules in the Azure Functions app

If you would like to add custom PowerShell modules that your script utilizes to perform different checks, you will be required to upload the modules from FTP. There is not any other way to upload custom modules other than uploading modules by accessing FTP of Functions App. To upload the custom modules in an Azure Function, you will be required to configure a service account that could be used to access Azure Functions FTP and then upload the modules at D:\Home\Site\wwwroot\{Functionname}.

Since the Azure Functions app is built using the Azure Web App Service, all the options such as deployment options are available. For example, you can configure deployment credentials, which, in turn, can be used to upload modules to FTP of Functions App.

To configure deployment credentials, click on “Functions Settings” as shown in the red square in the screenshot below:

Next, go to App Service Settings to configure deployment credentials. In FTP username and password dialogue box, enter the FTP username and password and save the settings. Once the FTP settings are saved, you can connect to Functions app FTP and upload your PowerShell modules.

Please make sure to upload PowerShell modules to D:\Home\Site\wwwroot\{Functionname} directory. When I was doing testing of uploading PowerShell modules to Functions app, I created a folder underneath {Functionname} by name “RDSModules” and then uploaded RDS Modules via FTP. I uploaded RDS PowerShell modules under D:\Home\Site\wwwroot\HttpTriggerFunction\RDSModules.

Loading custom modules from the Azure Functions app

Once you have uploaded PowerShell modules to D:\Home\Site\wwwroot\{functionname} by accessing FTP of the Functions app, go to Azure Portal > expand the function and enter the following commands in your Run.PS1 file:

Import-Module D:\Home\Site\wwwroot\HttpTriggerFunction\{RDSModules}

You need to replace the “RDSModules” directory with the folder name of your modules directory in the above command.

You should be able to import modules by using the command above and use PowerShell cmdlets that ship with the RDS modules.

Featured image: Shutterstock

Nirmal Sharma

Nirmal Sharma is a MCSEx3, MCITP and was awarded the Microsoft MVP award in Directory Services and Windows Networking. He specializes in Microsoft Azure, Office 365, Directory Services, Failover Clusters, Hyper-V, PowerShell Scripting and System Center products. Nirmal has been involved with Microsoft Technologies since 1994. In his spare time, he likes to help others and share some of his knowledge by writing tips and articles on various sites.

Published by
Nirmal Sharma

Recent Posts

Review: Identity verification solution Specops Secure Service Desk

Specops Secure Service Desk is an innovative solution for positively identifying a user who calls…

9 hours ago

Apple Silicon: What it means for the world of personal computing

Apple is moving away from Intel processors to use its own Apple Silicon processors to…

12 hours ago

RAID 0 vs. RAID 1: When to use each level and why

Two of the most popular RAID levels for improving performance are RAID 0 and RAID…

15 hours ago

Got cybersecurity tools? Good. Got too many? That may be a problem

Strength in numbers may not apply to cybersecurity tools. In fact, using too many tools…

1 day ago

Getting started with System Center Operations Manager

System Center Operations Manager can monitor your IT resources, but the tool is only as…

2 days ago

Microsoft 365 administration: Creating DNS records for email security

Microsoft 365 administration has many facets, but none is more important than configuring email. Here’s…

2 days ago