Azure Functions对PowerShell脚本的支持目前处于实验阶段。以下场景得到了支持:
1. Azure Functions将能够支持客户自带模块。这些模块将位于名为“modules”的文件夹中,该文件夹位于PowerShell脚本所在的同一目录中。在示例函数的
Kudu控制台中,目录结构如下:
![enter image description here](https://istack.dev59.com/je1Kvl.webp)
我们不支持客户使用 Install-Module
命令来安装他们自己的模块,但是客户可以将他们的模块上传到 modules
文件夹中。
modules
文件夹中的所有模块都将自动加载,因此客户无需显式使用 Import-Module
命令。
我们将支持 脚本、二进制 和 清单 模块。这些模块将驻留在 modules
文件夹内的平面结构中。一个示例布局如下:
![enter image description here](https://istack.dev59.com/Vj7k1m.webp)
针对您所要实现的内容,以下是一些建议的步骤,以确保加载 AWSPowerShell
模块。
在开发计算机上安装 AWSPowerShell
。您需要上传\AWSPowerShell\3.3.5.0
下的所有内容。
使用Kudu界面,将AWSPowerShell
的已安装依赖项上传到位于函数目录中的modules
文件夹中。要这样做,请打开函数应用程序的Portal UI并单击“Function App Settings”按钮。
接下来,单击“Go to Kudu”按钮启动Kudu控制台。您应该看到类似以下内容的快照:
![enter image description here](https://istack.dev59.com/uKAUmm.webp)
在cmd控制台提示符中,导航到您的函数文件夹,创建一个modules
目录,并将所有内容从\AWSPowerShell\3.3.5.0
上传到modules
目录中。
您最终应该得到一个模块文件夹,其中包含类似于下面快照的文件列表:
5. 运行您的函数。例如,给定以下用于我的函数的脚本,
```
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函数在多租户环境中执行。因此,存在以下已知注意事项:
1. 我们的基础架构会防止任何执行我们认为是安全风险的低级API的函数(例如交互式模式、主机凭据访问、注册表编辑等)。如果您使用的PowerShell脚本或模块调用了任何这些被阻止的API,则无法在函数中执行这些工作负载。尽管如此,我们的目标是支持尽可能多的场景,因此我们将根据客户需求量优先解除阻止的情况。
2. 我们当前在基础架构中安装了PowerShell版本4.0和Azure PowerShell 1.4。我们将很快升级这些版本。随着我们在Azure函数中增加对PowerShell的支持,模块套件可能会随时间升级或添加。这些预安装的模块可能会与您现有的模块发生冲突。
if (-not (Get-Module -Name "ScriptName"))
来检查脚本模块。我仍在努力尝试通过模块中的 Add-Type 可选加载 DLL。 - Rick Glos