如何在 Azure 函数中安装 PowerShell 模块

How to install a PowerShell module in an Azure Function

我需要 AWS 模块,它可以从 PS-Gallery 获得,但是当我尝试 运行 Azure Function 中的安装步骤时,它不起作用。 -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");
}

函数日志

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 之后也是如此!

修改后的代码

[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)

您需要通过将 -listavailable 添加到 get-module 调用来确保查找所有模块,而不仅仅是加载的模块。

您可能需要 bootstrap nuget 才能使安装模块在非交互式环境中工作。命令为:Get-PackageProvider -Name nuget -ForceBootstrap

如果您安装的存储库不受信任,您可能需要强制执行 install-module 命令。

例子

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

仅供参考:[Console]::WriteLine 在 PowerShell 中不被认为是自动化脚本的良好做法。尽量坚持Write-Verbose你可以这样强行Write-Verbose -message 'my message' -verbose

我怀疑原因是您没有指定要加载的模块的位置。当前 $env:PSModulePath 列为

WindowsPowerShell\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 SDKs\Azure\PowerShell\ServiceManagement\;
D:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\Storage\

我不能完全弄清楚它从哪里引用第一个,所以我不能把它们放在那里。所以我把它放在一起

$env:PSModulePath = $env:PSModulePath + ";d:\home\modules\"

import-module azured 
$out = Deploy-Template
[Console]::WriteLine($out)
Out-File -Encoding Ascii $Env:res -inputObject $out

这会加载模块(位于 d:\home\modules)并按预期工作

Get-PackageProvider -Name nuget -ForceBootstrap 不起作用的原因是未安装 PackageManagement 模块。但是已经安装了nuget。您需要直接与 kudu 交互才能以这种方式安装软件包。

我很高兴看到我不是唯一一个被函数逼疯的人 ;)

Azure Functions 对 PowerShell 脚本的支持目前处于实验阶段。支持以下场景:

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

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

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

  3. 我们将支持 scriptbinarymanifest 模块。这些模块将驻留在 modules 文件夹内的平面结构中。示例布局如下:

根据您要实现的目标,这里有一些建议的步骤可确保 AWSPowerShell 模块已加载。

  1. 在您的开发机器上本地安装 AWSPowerShell。您需要上传\AWSPowerShell.3.5.0

  2. 下的所有内容
  3. 使用 Kudu 界面,将 AWSPowerShell 的已安装依赖项上传到函数目录中的 modules 文件夹。为此,请打开您的函数应用程序的门户 UI,然后单击 函数应用程序设置 按钮。

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

  5. 在 cmd 控制台提示中,导航到您的函数文件夹,创建一个 modules 目录并将所有内容从 \AWSPowerShell.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)

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

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

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

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

关于更新 azurerm powershell 模块,有很多模块可以升级到最新版本 4.1.0,如果我们上传这些模块,将其放入平面目录将是一个问题。

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

包含 46 个文件夹。