How to install PowerShell module in Azure Function

I need the AWS module, which is available in PS-Gallery, but when I try to run the installation step inside Azure Function , it does not work. The -verbose argument flag does not write anything to the console.

What is the correct way to get and use additional PowerShell modules in Azure function?

Function code

[Console]::WriteLine("PowerShell Timer trigger function executed at:$(get-date)"); if (-not (Get-Module -Name "AWSPowerShell")) { [Console]::WriteLine("AWSPowerShell not installed"); Install-Module -Name AWSPowerShell -Scope CurrentUser -ErrorAction Continue -Verbose } if (-not (Get-Module -Name "AWSPowerShell")){ [Console]::WriteLine("AWSPowerShell install step failed"); } 

Function Log

 2016-06-09T11:24:31.108 Function started (Id=e09be687-2e13-4754-942e-eab75c8516e5) 2016-06-09T11:24:32.788 Powershell Timer trigger function executed at:06/09/2016 11:24:32 AWSPowerShell not installed AWSPowerShell install step failed 2016-06-09T11:24:32.788 Function completed (Success, Id=e09be687-2e13-4754-942e-eab75c8516e5) 

Update

In response to @travis, I added suggested code, including the nuget package manager string. It still didn't work. I added another debug line and found that there were no module providers, even after trying to add nuget!

Revised code

 [Console]::WriteLine("Powershell Timer trigger function executed at:$(get-date)"); Get-PackageProvider -Name nuget -ForceBootstrap $pkg=Get-PackageProvider -ListAvailable if ($pkg.Count -lt 1){ [Console]::WriteLine("No providers")} if (-not (Get-Module -listavailable -Name "AWSPowerShell")) { [Console]::WriteLine("AWSPowerShell not installed"); Install-Module -Name AWSPowerShell -Scope CurrentUser -ErrorAction Continue -Verbose -force } if (-not (Get-Module -listavailable -Name "AWSPowerShell")){ [Console]::WriteLine("AWSPowerShell install step failed"); } Import-Module AWSPowerShell 

Revised magazine

 2016-06-09T17:54:03.859 Powershell Timer trigger function executed at:06/09/2016 17:54:02 No providers AWSPowerShell not installed AWSPowerShell install step failed 2016-06-09T17:54:03.859 Function completed (Success, Id=80efb9fc-5e91-45f9-ab58-3b71fcd764af) 
+5
source share
4 answers

Support for Azure features for PowerShell scripts is currently under experimental development. The following scenarios are supported:

  • Azure features will be able to support customers who bring their own modules. These modules will be located in a folder called modules , which is in the same directory as the PowerShell script. In the Kudu console for the Function example, the directory structure will look like this:

enter image description here

  1. We will not support clients installing their own modules using the Install-Module cmdlet, however clients can download their modules to the modules folder.

  2. All modules in the modules folder will load automatically, so clients do not have to explicitly use the Import-Module cmdlet.

  3. We will support script, binary and manifest modules. These modules will be located in a flat structure in the modules folder. An example layout is as follows:

    enter image description here

In the context of what you are trying to achieve, we’ll take a look at some suggested steps to make sure the AWSPowerShell module AWSPowerShell loaded.

  • Install AWSPowerShell locally on your development machine. You will need to load all the contents in \AWSPowerShell\3.3.5.0

  • Using the Kudu interface, load the installed AWSPowerShell dependencies into the modules folder in your function directory. To do this, open the portal user interface for your application application and click the Application Functional Settings button.

  • Then click the Go to Kudu button to start the Kudu console. You should see a picture similar to the following,

    enter image description here

  • At the cmd prompt, go to your Function folder, create the modules directory and load all the contents from \AWSPowerShell\3.3.5.0 into the modules directory.

You should have a modules folder in which there is a list of files similar to the screenshot below:

enter image description here

  1. Run the function. For example, given the following script for my function,

    if (-not (Get-Module -Name "AWSPowerShell")) { Write-Output "AWSPowerShell not installed"; } else { Write-Output "AWSPowerShell installed"; }

when executed, the log output is as follows:

 2016-10-11T18:26:01.486 Function started (Id=582b69aa-6236-436d-81c5-c08ada8ae674) 2016-10-11T18:26:03.267 Loaded modules: /AWSPowerShell/modules/AWSPowerShell.psd1 /AWSPowerShell/modules/AWSPowerShell.dll /AWSPowerShell/modules/AWSSDK.APIGateway.dll /AWSPowerShell/modules/AWSSDK.ApplicationAutoScaling.dll /AWSPowerShell/modules/AWSSDK.ApplicationDiscoveryService.dll /AWSPowerShell/modules/AWSSDK.AutoScaling.dll /AWSPowerShell/modules/AWSSDK.AWSMarketplaceCommerceAnalytics.dll /AWSPowerShell/modules/AWSSDK.AWSMarketplaceMetering.dll /AWSPowerShell/modules/AWSSDK.AWSSupport.dll /AWSPowerShell/modules/AWSSDK.CertificateManager.dll /AWSPowerShell/modules/AWSSDK.CloudFormation.dll /AWSPowerShell/modules/AWSSDK.CloudFront.dll /AWSPowerShell/modules/AWSSDK.CloudHSM.dll /AWSPowerShell/modules/AWSSDK.CloudSearch.dll /AWSPowerShell/modules/AWSSDK.CloudSearchDomain.dll /AWSPowerShell/modules/AWSSDK.CloudTrail.dll /AWSPowerShell/modules/AWSSDK.CloudWatch.dll /AWSPowerShell/modules/AWSSDK.CloudWatchEvents.dll /AWSPowerShell/modules/AWSSDK.CloudWatchLogs.dll /AWSPowerShell/modules/AWSSDK.CodeCommit.dll /AWSPowerShell/modules/AWSSDK.CodeDeploy.dll /AWSPowerShell/modules/AWSSDK.CodePipeline.dll /AWSPowerShell/modules/AWSSDK.CognitoIdentity.dll /AWSPowerShell/modules/AWSSDK.CognitoIdentityProvider.dll /AWSPowerShell/modules/AWSSDK.ConfigService.dll /AWSPowerShell/modules/AWSSDK.Core.dll /AWSPowerShell/modules/AWSSDK.DatabaseMigrationService.dll /AWSPowerShell/modules/AWSSDK.DataPipeline.dll /AWSPowerShell/modules/AWSSDK.DeviceFarm.dll /AWSPowerShell/modules/AWSSDK.DirectConnect.dll /AWSPowerShell/modules/AWSSDK.DirectoryService.dll /AWSPowerShell/modules/AWSSDK.DynamoDBv2.dll /AWSPowerShell/modules/AWSSDK.EC2.dll /AWSPowerShell/modules/AWSSDK.ECR.dll /AWSPowerShell/modules/AWSSDK.ECS.dll /AWSPowerShell/modules/AWSSDK.ElastiCache.dll /AWSPowerShell/modules/AWSSDK.ElasticBeanstalk.dll /AWSPowerShell/modules/AWSSDK.ElasticFileSystem.dll /AWSPowerShell/modules/AWSSDK.ElasticLoadBalancing.dll /AWSPowerShell/modules/AWSSDK.ElasticLoadBalancingV2.dll /AWSPowerShell/modules/AWSSDK.ElasticMapReduce.dll /AWSPowerShell/modules/AWSSDK.Elasticsearch.dll /AWSPowerShell/modules/AWSSDK.ElasticTranscoder.dll /AWSPowerShell/modules/AWSSDK.GameLift.dll /AWSPowerShell/modules/AWSSDK.IdentityManagement.dll /AWSPowerShell/modules/AWSSDK.ImportExport.dll /AWSPowerShell/modules/AWSSDK.Inspector.dll /AWSPowerShell/modules/AWSSDK.IoT.dll /AWSPowerShell/modules/AWSSDK.IotData.dll /AWSPowerShell/modules/AWSSDK.KeyManagementService.dll /AWSPowerShell/modules/AWSSDK.Kinesis.dll /AWSPowerShell/modules/AWSSDK.KinesisAnalytics.dll /AWSPowerShell/modules/AWSSDK.KinesisFirehose.dll /AWSPowerShell/modules/AWSSDK.Lambda.dll /AWSPowerShell/modules/AWSSDK.MachineLearning.dll /AWSPowerShell/modules/AWSSDK.MobileAnalytics.dll /AWSPowerShell/modules/AWSSDK.OpsWorks.dll /AWSPowerShell/modules/AWSSDK.RDS.dll /AWSPowerShell/modules/AWSSDK.Redshift.dll /AWSPowerShell/modules/AWSSDK.Route53.dll /AWSPowerShell/modules/AWSSDK.Route53Domains.dll /AWSPowerShell/modules/AWSSDK.S3.dll /AWSPowerShell/modules/AWSSDK.SecurityToken.dll /AWSPowerShell/modules/AWSSDK.ServiceCatalog.dll /AWSPowerShell/modules/AWSSDK.SimpleEmail.dll /AWSPowerShell/modules/AWSSDK.SimpleNotificationService.dll /AWSPowerShell/modules/AWSSDK.SimpleSystemsManagement.dll /AWSPowerShell/modules/AWSSDK.SimpleWorkflow.dll /AWSPowerShell/modules/AWSSDK.Snowball.dll /AWSPowerShell/modules/AWSSDK.SQS.dll /AWSPowerShell/modules/AWSSDK.StorageGateway.dll /AWSPowerShell/modules/AWSSDK.WAF.dll /AWSPowerShell/modules/AWSSDK.WorkSpaces.dll /AWSPowerShell/modules/log4net.dll /AWSPowerShell/modules/AWSPowerShellCompleters.psm1 2016-10-11T18:27:21.265 AWSPowerShell installed 2016-10-11T18:27:21.464 Function completed (Success, Id=582b69aa-6236-436d-81c5-c08ada8ae674) 

Note. The function takes some time to complete due to the fact that all modules are loaded at runtime.

It’s important to remember that unlike most IaaS settings, the Azure feature runs in a multi-tenant environment. Thus, the following known reservations remain:

  • Our infrastructure protects against any function that performs low-level APIs that we consider to be security risks (for example, interactive modes, access to host credentials, registry changes, etc.). If the PowerShell script or module you are using calls any of these blocked APIs, you will not be able to execute these workloads in functions. However, our goal is to support as many scenarios as possible, so we will give priority to unlocking scenarios based on the volume of demand from our customers.

  • PowerShell 4.0 and Azure PowerShell 1.4 are currently installed on our infrastructure. We will update these versions soon. As additional PowerShell support is added to Azure Functions, modules can be updated or added over time. There is an empty possibility that these pre-installed modules may conflict with your existing modules.

+5
source

You need to make sure that you are looking for all modules, not just loaded modules, by adding -listavailable to the get-module call.

You may need to restart nuget to install the module to work in non-interactive environments. Command: Get-PackageProvider -Name nuget -ForceBootstrap

If the repository you are installing from is not trusted, you may need to force the install-module command.

Example

 [Console]::WriteLine("Powershell Timer trigger function executed at:$(get-date)"); if (-not (Get-Module -listavailable -Name "AWSPowerShell")) { [Console]::WriteLine("AWSPowerShell not installed"); Install-Module -Name AWSPowerShell -Scope CurrentUser -ErrorAction Continue -Verbose -force } if (-not (Get-Module -listavailable -Name "AWSPowerShell")){ [Console]::WriteLine("AWSPowerShell install step failed"); } 

FYI: [Console] :: WriteLine is not considered good practice in PowerShell for automated scripts. Try to stick with Write-Verbose , you can force it like this Write-Verbose -message 'my message' -verbose

+1
source

Regarding updating the azurerm powershell modules, there are many modules to get to the latest version 4.1.0, which, if we download, will be a problem to put in a flat directory.

C: \ Program Files (x86) \ Microsoft SDK \ Azure \ PowerShell \ ResourceManager \ AzureResourceManager

contains 46 folders.

+1
source

I suspect that the reason is because you do not specify a location for the modules from which it will load. Current $env:PSModulePath is listed as

Windows PowerShell \ Modules
D: \ Program Files (x86) \ WindowsPowerShell \ Modules; D: \ Windows \ system32 \ WindowsPowerShell \ v1.0 \ Modules \;
d: \ Program Files \ Microsoft Security Client \ MpProvider \;
D: \ Program Files \ Microsoft Message Analyzer \ PowerShell \;
D: \ Program Files \ WindowsPowerShell \ Modules \;
D: \ Program Files (x86) \ MicrosoftSDKs \ Azure \ PowerShell \ ResourceManager \ AzureResourceManager \;
D: \ Program Files (x86) \ Microsoft SDK \ Azure \ PowerShell \ ServiceManagement \;
D: \ Program Files (x86) \ Microsoft SDK \ Azure \ PowerShell \ Storage \

I cannot fully understand where it refers to the first, so I could not put them. So I put this instead

 $env:PSModulePath = $env:PSModulePath + ";d:\home\modules\" import-module azured $out = Deploy-Template [Console]::WriteLine($out) Out-File -Encoding Ascii $Env:res -inputObject $out 

Loads a module (located in d: \ home \ modules) and works as expected

The reason that Get-PackageProvider -Name nuget -ForceBootstrap does not work is because the PackageManagement module PackageManagement not installed. However, nuget is already installed. You will need to interact with kudu directly to install packages this way.

I am glad to see that I am not the only one who is controlled by a little nuts by function, though;)

0
source

All Articles