Share via

Azure Functions using Powershell - How to pre-load powershell modules to be used during execution.

mitesh patel 6 Reputation points
2024-11-13T17:36:04.2966667+00:00

Hello Geeks,

Here's the scenario - I'm executing Azure functions (PowerShell code) using time trigger running on Consumption App service plan. I would like to execute this function every minute. The functions basically uses bunch of Azure powershell commands to carry out the work needed. I have described the dependent Azure modules in requirements.psd1 file and have also added Import-Module statements for each of the modules in profile.ps1 file.

Expectation - After/During the initial cold start, the modules are loaded so that the subsequent function executions every minute need not load the modules and instead use the already loaded modules.

Actual - I see in app insights that for every function execution, the modules are loaded all over again leading to higher execution times. The actual script would take < 10 sec to execute however with modules being loaded the overall execution time reaches to nearly a minute.

How to ensure that the modules are always pre-loaded so that the function executions take relatively less time to execute? Thanks!

Azure Functions
Azure Functions

An Azure service that provides an event-driven serverless compute platform.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

2 answers

Sort by: Most helpful
  1. Rich Matheisen 48,116 Reputation points
    2024-11-14T16:12:25.0566667+00:00

    Why not just start the script once? Put your code inside a "while" loop and, as the last statement, wait 60 seconds before continuing? You can do any cleanup of data at the top (or bottom) of the code within the loop.

    Was this answer helpful?

    0 comments No comments

  2. Rich Matheisen 48,116 Reputation points
    2024-11-13T20:00:09.5733333+00:00

    Not that I know of. Modules have data that is persistent. Reusing the data would mean that it's possible to leak information that should remain private, and it also has a more general problem that the data are not thread-safe. E.g., you run PS session #1 that loads the module. Then PS session #2 and #3 start and run concurrently. The data left over from #1 is available to #2 and #3, and the data from #2 and #3 are subject to being overwritten by each other.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.