You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/azure-functions/functions-reference-powershell.md
+73-85Lines changed: 73 additions & 85 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -456,93 +456,106 @@ The function app restarts after the change is made to the configuration.
456
456
457
457
## Dependency management
458
458
459
-
Functions lets you leverage [PowerShell gallery](https://www.powershellgallery.com) for managing dependencies. With dependency management enabled, the requirements.psd1 file is used to automatically download required modules. You enable this behavior by setting the `managedDependency` property to `true` in the root of the [host.json file](functions-host-json.md), as in the following example:
459
+
Managing modules in Azure Functions written in PowerShell can be approached in two ways: using the Managed Dependencies feature or including the modules directly in your app content. Each method has its own advantages, and choosing the right one depends on your specific needs.
460
460
461
-
```json
462
-
{
463
-
"managedDependency": {
464
-
"enabled": true
465
-
}
466
-
}
467
-
```
461
+
### Choosing the Right Module Management Approach
468
462
469
-
When you create a new PowerShell functions project, dependency management is enabled by default, with the Azure [`Az` module](/powershell/azure/new-azureps-module-az) included. The maximum number of modules currently supported is 10. The supported syntax is *`MajorNumber.*`* or exact module version, as shown in the following requirements.psd1 example:
463
+
**Managed Dependencies Feature**
464
+
-**Simplified initial installation**: Automatically handles module installation based on your `requirements.psd1` file.
465
+
-**Auto-upgrades**: Modules are updated automatically, including security fixes, without requiring manual intervention.
470
466
471
-
```powershell
472
-
@{
473
-
Az = '1.*'
474
-
SqlServer = '21.1.18147'
475
-
}
476
-
```
467
+
**Including Modules in App Content**
468
+
-**No dependency on the PowerShell Gallery**: Modules are bundled with your app, eliminating external dependencies.
469
+
-**More control**: Avoids the risk of auto-upgrade regressions, giving you full control over which module versions are used.
470
+
-**Compatibility**: Works on Flex Consumption and is recommended for other Linux SKUs.
477
471
478
-
When you update the requirements.psd1 file, updated modules are installed after a restart.
472
+
### Managed Dependencies Feature
479
473
480
-
### Target specific versions
474
+
The Managed Dependencies feature allows Azure Functions to automatically download and manage PowerShell modules specified in the `requirements.psd1` file. This is enabled by default in new PowerShell function apps.
481
475
482
-
You may want to target a specific version of a module in your requirements.psd1 file. For example, if you wanted to use an older version of Az.Accounts than the one in the included Az module, you would need to target a specific version as shown in the following example:
476
+
#### Enabling Managed Dependencies
483
477
484
-
```powershell
485
-
@{
486
-
'Az.Accounts' = '1.9.5'
478
+
To enable managed dependencies, set the `managedDependency` property to `true` in the `host.json` file:
479
+
480
+
```json
481
+
{
482
+
"managedDependency": {
483
+
"enabled": true
484
+
}
487
485
}
488
486
```
489
487
490
-
In this case, you also need to add an import statement to the top of your profile.ps1 file, which looks like the following example:
When targeting specific module versions, it’s important to follow both steps below to ensure the correct module version is loaded:
495
491
496
-
In this way, the older version of the Az.Account module is loaded first when the function is started.
492
+
1.**Specify the module version in `requirements.psd1`:**
497
493
498
-
### Dependency management considerations
494
+
```powershell
495
+
@{
496
+
'Az.Accounts' = '1.9.5'
497
+
}
498
+
```
499
499
500
-
The following considerations apply when using dependency management:
500
+
2. **Add an import statement to `profile.ps1`:**
501
501
502
-
+ Managed dependencies requires access to `https://www.powershellgallery.com` to download modules. When running locally, make sure that the runtime can access this URL by adding any required firewall rules.
+ Managed dependencies currently don't support modules that require the user to accept a license, either by accepting the license interactively, or by providing `-AcceptLicense` switch when invoking `Install-Module`.
506
+
This ensures the specified version is loaded when your function starts.
505
507
506
-
+ Managed dependencies aren't supported when you host your function app in a [Flex Consumption plan](flex-consumption-plan.md). You must instead [define your own custom modules](#custom-modules).
508
+
#### Dependency Management Considerations
507
509
508
-
### Dependency management app settings
510
+
- **Internet Access**: Managed dependencies require access to `https://www.powershellgallery.com` to download modules. Ensure that your environment allows this access.
511
+
- **License Acceptance**: Modules that require license acceptance are not supported by managed dependencies.
512
+
- **Flex Consumption Plan**: Managed dependencies are not supported in the Flex Consumption plan. Use custom modules instead.
509
513
510
-
The following application settings can be used to change how the managed dependencies are downloaded and installed.
514
+
#### Dependency Management App Settings
511
515
512
-
| Function App setting | Default value | Description |
|**MDMaxBackgroundUpgradePeriod**|`7.00:00:00` (seven days) | Controls the background update period for PowerShell function apps. To learn more, see [MDMaxBackgroundUpgradePeriod](functions-app-settings.md#mdmaxbackgroundupgradeperiod). |
515
-
|**MDNewSnapshotCheckPeriod**|`01:00:00` (one hour) | Specifies how often each PowerShell worker checks whether managed dependency upgrades have been installed. To learn more, see [MDNewSnapshotCheckPeriod](functions-app-settings.md#mdnewsnapshotcheckperiod).|
516
-
|**MDMinBackgroundUpgradePeriod**|`1.00:00:00` (one day) | The period of time after a previous upgrade check before another upgrade check is started. To learn more, see [MDMinBackgroundUpgradePeriod](functions-app-settings.md#mdminbackgroundupgradeperiod).|
516
+
You can configure how managed dependencies are downloaded and installed using the following app settings:
517
517
518
-
Essentially, your app upgrade starts within `MDMaxBackgroundUpgradePeriod`, and the upgrade process completes within approximately the `MDNewSnapshotCheckPeriod`.
| **MDMaxBackgroundUpgradePeriod** | `7.00:00:00` (seven days) | Controls the background update period for PowerShell function apps. |
521
+
| **MDNewSnapshotCheckPeriod** | `01:00:00` (one hour) | Specifies how often the PowerShell worker checks for updates. |
522
+
| **MDMinBackgroundUpgradePeriod** | `1.00:00:00` (one day) | Minimum time between upgrade checks. |
519
523
520
-
##Custom modules
524
+
### Including Modules in App Content
521
525
522
-
Leveraging your own custom modules in Azure Functions differs from how you would do it normally for PowerShell.
526
+
For more control over your module versions and to avoid dependencies on external resources, you can include modules directly in your function app’s content.
523
527
524
-
On your local computer, the module gets installed in one of the globally available folders in your `$env:PSModulePath`. When running in Azure, you don't have access to the modules installed on your machine. This means that the `$env:PSModulePath` for a PowerShell function app differs from `$env:PSModulePath` in a regular PowerShell script.
528
+
#### Function App-Level Modules Folder
525
529
526
-
In Functions, `PSModulePath` contains two paths:
530
+
To include custom modules:
527
531
528
-
* A `Modules` folder that exists at the root of your function app.
529
-
* A path to a `Modules` folder that is controlled by the PowerShell language worker.
532
+
1. **Create a `Modules` folder** at the root of your function app.
530
533
531
-
### Function app-level modules folder
534
+
```powershell
535
+
mkdir ./Modules
536
+
```
532
537
533
-
To use custom modules, you can place modules on which your functions depend in a `Modules` folder. From this folder, modules are automatically available to the functions runtime. Any function in the function app can use these modules.
538
+
2. **Copy modules to the `Modules` folder** using one of the following methods:
534
539
535
-
> [!NOTE]
536
-
> Modules specified in the [requirements.psd1 file](#dependency-management) are automatically downloaded and included in the path so you don't need to include them in the modules folder. These are stored locally in the `$env:LOCALAPPDATA/AzureFunctions` folder and in the `/data/ManagedDependencies` folder when run in the cloud.
540
+
- **If modules are already available locally**:
537
541
538
-
To take advantage of the custom module feature, create a `Modules` folder in the root of your function app. Copy the modules you want to use in your functions to this location.
Your function app should have the following structure:
546
559
547
560
```
548
561
PSFunctionApp
@@ -558,34 +571,7 @@ PSFunctionApp
558
571
| - requirements.psd1
559
572
```
560
573
561
-
When you start your function app, the PowerShell language worker adds this `Modules` folder to the `$env:PSModulePath` so that you can rely on module autoloading just as you would in a regular PowerShell script.
562
-
563
-
### Language worker level modules folder
564
-
565
-
Several modules are commonly used by the PowerShell language worker. These modules are defined in the last position of `PSModulePath`.
566
-
567
-
The current list of modules is as follows:
568
-
569
-
*[Microsoft.PowerShell.Archive](https://www.powershellgallery.com/packages/Microsoft.PowerShell.Archive): module used for working with archives, like `.zip`, `.nupkg`, and others.
570
-
***ThreadJob**: A thread-based implementation of the PowerShell job APIs.
571
-
572
-
By default, Functions uses the most recent version of these modules. To use a specific module version, put that specific version in the `Modules` folder of your function app.
573
-
574
-
## Environment variables
575
-
576
-
In Functions, [app settings](functions-app-settings.md), such as service connection strings, are exposed as environment variables during execution. You can access these settings using `$env:NAME_OF_ENV_VAR`, as shown in the following example:
577
-
578
-
```powershell
579
-
param($myTimer)
580
-
581
-
Write-Host "PowerShell timer trigger function ran! $(Get-Date)"
When running locally, app settings are read from the [local.settings.json](functions-develop-local.md#local-settings-file) project file.
574
+
Modules in the `Modules` folder are automatically available to the runtime.
589
575
590
576
## Concurrency
591
577
@@ -693,9 +679,11 @@ When you work with PowerShell functions, be aware of the considerations in the f
693
679
694
680
When developing Azure Functions in the [serverless hosting model](consumption-plan.md), cold starts are a reality. *Cold start* refers to period of time it takes for your function app to start running to process a request. Cold start happens more frequently in the Consumption plan because your function app gets shut down during periods of inactivity.
695
681
696
-
### Bundle modules instead of using Install-Module
682
+
#### Avoid Using Install-Module
683
+
684
+
Running `Install-Module` in your function script on each invocation can cause performance issues. Instead, use `Save-Module` or `Save-PSResource` before publishing your function app to bundle the necessary modules.
697
685
698
-
Your script is run on every invocation. Avoid using `Install-Module` in your script. Instead use `Save-Module` before publishing so that your function doesn't have to waste time downloading the module. If cold starts are impacting your functions, consider deploying your function app to an [App Service plan](dedicated-plan.md) set to *always on* or to a [Premium plan](functions-premium-plan.md).
686
+
For more information, refer to the [Module Management](#module-management) section.
0 commit comments