如何在Azure功能中安装PowerShell模块

Ste*_*cke 9 powershell azure azure-functions

我需要可以从PS-Gallery获得的AWS模块,但是当我尝试在Azure功能中运行安装步骤时,它不起作用.-verbose参数标志未向控制台写入任何内容.

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

功能代码

[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");
}
Run Code Online (Sandbox Code Playgroud)

功能日志

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)
Run Code Online (Sandbox Code Playgroud)

更新

根据@travis的回答,我添加了建议的代码,包括nuget包管理器行.这仍然无效.我添加了另一个调试行,发现即使在尝试添加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
Run Code Online (Sandbox Code Playgroud)

修改日志

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)
Run Code Online (Sandbox Code Playgroud)

Lin*_*Toh 9

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

  1. Azure Functions将能够支持客户自带模块.这些模块将驻留在名为的文件夹中,该文件夹modules位于PowerShell脚本所在的同一目录中.在示例Function 的Kudu控制台中,目录结构如下所示,

在此输入图像描述

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

  2. modules文件夹中的所有模块都将自动加载,因此客户无需显式使用该Import-Modulecmdlet.

  3. 我们将支持脚本,二进制和清单模块.这些模块将驻留在modules文件夹内的平面结构中.示例布局如下:

    在此输入图像描述

在您要实现的目标的上下文中,这里是一些建议的步骤,以确保AWSPowerShell加载模块.

  1. AWSPowerShell在开发计算机上本地安装.您需要上传所有内容\AWSPowerShell\3.3.5.0

  2. 使用Kudu界面,将已安装的依赖项上传AWSPowerShell到modules驻留在Function目录中的文件夹.为此,请打开功能应用程序的Portal UI,然后单击" 功能应用程序设置"按钮.

  3. 接下来,单击Go to Kudu按钮启动Kudu控制台.您应该看到类似于以下内容的快照,

    在此输入图像描述

  4. 在cmd控制台提示符下,导航到Function的文件夹,创建modules目录并将所有内容上传\AWSPowerShell\3.3.5.0到modules目录.

您应该得到一个模块文件夹,其中包含与下面的快照类似的文件列表:

在此输入图像描述

  1. 运行你的功能.例如,给出我的函数的以下脚本,

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

执行时,日志输出如下,

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)
Run Code Online (Sandbox Code Playgroud)

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

它记住,不像大多数IaaS的设置,湛蓝的功能是在多租户环境中执行是非常重要的.因此,仍然存在以下已知的警告:

  1. 我们的基础设施可以防止执行我们认为存在安全风险的低级API的任何功能(例如交互模式,主机凭证访问,注册表编辑等).如果您使用的PowerShell脚本或模块调用任何这些被阻止的API,您将无法在函数中执行这些工作负载.尽管如此,我们的目标是尽可能多地支持场景,因此我们将根据客户的需求量优先解锁场景.

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

  • 此外,您还可以将自定义模块目录移动到树中,以便可以在同一个功能应用程序中的功能之间共享相同的路径. (2认同)