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

时间:2016-06-09 11:31:33

标签: powershell azure azure-functions

我需要可以从PS-Gallery获得的AWS模块,但是当我尝试在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)

更新

Per @ 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)

4 个答案:

答案 0 :(得分:9)

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

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

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

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

    3. 我们将支持脚本二进制清单模块。这些模块将位于modules文件夹中的平面结构中。示例布局如下:

      enter image description here

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

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

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

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

        enter image description here

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

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

        enter image description here

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

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

        2. 执行时,日志输出如下,

          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功能正在多租户环境中执行。因此,仍然存在以下已知的警告:

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

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

答案 1 :(得分:1)

您需要确保通过向-listavailable电话添加get-module来查找所有模块,而不仅仅是已加载的模块。

您可能需要引导nuget for install-module才能在非交互式环境中工作。命令是: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

那样强迫它

答案 2 :(得分:1)

如何更新azurerm powershell模块,有很多模块可以访问最新版本4.1.0,如果我们上传将是一个问题放在一个平面目录。

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

包含46个文件夹。

答案 3 :(得分:0)

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

  

WindowsPowerShell \模块;
  D:\ Program Files(x86)\ WindowsPowerShell \ Modules;   d:\ WINDOWS \ SYSTEM32 \ WindowsPowerShell \ V1.0 \模块\;
  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交互以便以这种方式安装软件包。

我很高兴看到我不是唯一一个被功能稍微惹恼的人;)