如何在Azure函数中安装PowerShell模块 [英] How to install a PowerShell module in an Azure Function

查看:69
本文介绍了如何在Azure函数中安装PowerShell模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个AWS模块,该模块可从PS-Gallery获得,但是当我尝试在

I need the AWS module, which is available from the PS-Gallery but when I try to run the install step inside an Azure Function, it doesn't work. The -verbose argument flag isn't writing anything to the console.

在Azure函数中获取和使用其他PowerShell模块的正确方法是什么?

What is the right way to get and use additional PowerShell modules within an Azure function?

[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");
}

功能日志

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)


更新

每个@travis的答案中,我添加了建议的代码,包括nuget软件包管理器行.这仍然没有用.我添加了另一条调试行,发现即使尝试添加nuget,也没有模块提供程序!


Update

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

[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

修订后的日志

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)

推荐答案

Azure Functions对PowerShell脚本的支持目前处于试验阶段.支持以下方案:

Azure Functions support for PowerShell scripting is currently in an experimental stage. The following scenarios are supported:

  1. Azure Functions将能够支持客户带来他们自己的模块.这些模块将驻留在名为modules的文件夹中,该文件夹位于PowerShell脚本所在的目录中.在 Kudu 控制台中,示例函数的目录结构看起来像像以下内容一样,
  1. Azure Functions will be able to support customers bringing their own modules. These modules will reside in a folder named modules that is located in the same directory where the PowerShell script resides. In the Kudu console for a sample Function, the directory structure would look like the following,

  1. 我们不支持客户使用Install-Module cmdlet安装自己的模块,但是,客户可以将其模块上传到modules文件夹中.

  1. We will not support customers installing their own modules using the Install-Module cmdlet, however, customers may upload their modules into the modules folder.

modules文件夹中的所有模块将自动加载,因此客户不必显式使用Import-Module cmdlet.

All modules in the modules folder will be loaded automatically, so customers will not have to explicitly use the Import-Module cmdlet.

我们将支持 script binary 清单模块.这些模块将驻留在modules文件夹中的平面结构中.布局示例如下:

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

根据您要实现的目标,以下是一些建议步骤,以确保已加载AWSPowerShell模块.

In the context of what you are trying to achieve, here are some suggested steps to make sure that the AWSPowerShell module is loaded.

  1. 在开发计算机上本地安装AWSPowerShell.您将需要在\AWSPowerShell\3.3.5.0

使用Kudu界面,将已安装的AWSPowerShell依赖项上载到函数目录中的modules文件夹中.为此,请打开您的Function App的门户网站UI,然后单击 Function App Settings 按钮.

Using the Kudu interface, upload the installed dependencies of AWSPowerShell to a modules folder residing in your Function's directory. To do so, open the the Portal UI for your Function App and click on the Function App Settings button.

下一步,单击转到Kudu 按钮以启动Kudu控制台.您应该看到类似于以下内容的快照

Next, click on the Go to Kudu button to launch the Kudu console. You should see a snapshot similar to the following,

在cmd控制台提示符下,导航到函数"的文件夹,创建一个modules目录,并将所有内容从\AWSPowerShell\3.3.5.0上载到modules目录.

In the cmd console prompt, navigate to your Function's folder, create a modules directory and upload all the contents from \AWSPowerShell\3.3.5.0 to the modules directory.

您应该最终得到一个modules文件夹,其中包含类似于以下快照的文件列表:

You should end up with a modules folder that has a list of files similar to the snapshot below:

  1. 运行您的功能.例如,给定以下用于我的Function的脚本,

  1. Run your Function. For instance, given the following script for my Function,

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

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)

注意:由于所有模块都是在运行时加载的,因此该函数需要一段时间才能完成.

请记住,与大多数IaaS设置不同,Azure Function在多租户环境中执行.因此,仍然存在以下已知警告:

It is important to bear in mind that unlike most IaaS setup, an Azure Function is executing in a multi-tenant environment. As such, there remains the following known caveats:

  1. 我们的基础架构可防止任何执行被视为安全风险的执行低级API的功能(例如,交互模式,主机凭据访问,注册表编辑等).如果您正在使用的PowerShell脚本或模块调用了这些阻止的API中的任何一个,您将无法在函数"中执行这些工作负载.尽管如此,我们的目标是支持尽可能多的方案,因此我们将根据客户的需求量来优先安排无阻塞方案.

  1. Our infrastructure guards against any Function executing low-level APIs that we deem as security risks (e.g. interactive modes, host credentials access, registry edits, and etc.). If the PowerShell script or module you are using calls any of those blocked APIs, you will not be able to execute those workloads in Functions. Nonetheless, our goal is to support as many scenarios as possible, so we will prioritize unblocking scenarios based on the demand volume from our customers.

我们当前在基础结构中安装了PowerShell版本4.0和Azure PowerShell 1.4.我们将尽快升级这些版本.随着我们在Azure Functions中添加对PowerShell的更多支持,模块套件可能会随着时间的推移而升级或添加.这些预安装的模块极有可能与您现有的模块冲突.

We currently have PowerShell version 4.0 and Azure PowerShell 1.4 installed in our infrastructure. We will upgrade these versions soon. As we add more support for PowerShell in Azure Functions, module suites may be upgraded or added over time. There is a remote possibility that these pre-installed modules may conflict with your existing modules.

这篇关于如何在Azure函数中安装PowerShell模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆