【Azure Function】Function App和Powershell 集成问题, 如何安装PowerShell的依赖模块

简介: 【Azure Function】Function App和Powershell 集成问题, 如何安装PowerShell的依赖模块

问题描述

在Azure Function中创建一个PowerShell的函数后,其中使用了Get-AzMaintenanceUpdate,New-AzApplyUpdate 等指令,但是在执行时错误。错误截图:

 

问题分析及解决

第一步:分析错误日志

在错误截图中,可以清晰的发现问题【ERROR: The term 'Get-AzMaintenanceUpdate' is not recognized as the name of a cmdlet, function, script file, or operable program. 】这里正好说明了在Function App所运行的实例中,没有安装支持Get-AzMaintenanceUpdate命令的模块包。网上搜索发现,这个方法属于Az.Maintenance包。所以接下来就是需要在Azure Function App中安装Az包。

 

第二步:如何安装所需要的PowerShell依赖包

方式一:在“Azure Functions PowerShell 开发人员指南”一文的"依赖项管理"中,谈论到可以使用 requirements.psd1 文件加载依赖性。

依赖项管理

Functions 允许你利用 PowerShell 库来管理依赖项。 启用依赖项管理后,使用 requirements.psd1 文件来自动下载所需的模块。 启用此行为的方法是:在 host.json 文件的根目录中,将 managedDependency 属性设置为 true,如以下示例所示:

{
  "managedDependency": {
          "enabled": true
       }
}

创建新的 PowerShell 函数项目时,默认情况下会启用依赖项管理,并且会包括 Azure Az 模块当前支持的最大模块数为 10。 支持的语法为 MajorNumber .* 或确切的模块版本,如以下 requirements.psd1 示例所示:

@{

 

Az = '1.*'
    SqlServer = '21.1.18147'

}

更新 requirements.psd1 文件时,会在重启后安装更新的模块。

修改成功后,在Kudu中所见的效果如下:

 

方式二:上传本地Az.Maintenance包文件到Function App 站点中,详细步骤为

  1. 在本地 PowerShell 安装Az.Maintenance这个moudle,安装好之后找到这个安装的文件夹
  2. 登录到kudu,在 CMD>site>wwwroot><your Function Name>这个目录下创建一个名为moudles 的文件夹, 把第一步中安装好的这个moudle里面的内容上传到这个文件夹下
  3. 找到执行文件,执行导入模块指令
Import-Module "D:\home\site\wwwroot\<your Function Name>\modules\Az.Maintenance.psd1"

 

附录一:Function App中所执行的PowerShell脚本

# Input bindings are passed in via param block.
param($Timer)
# Check if any maintenance updates are available for your dedicated host
$isMaintenance = Get-AzMaintenanceUpdate `
    -ResourceGroupName MaintenanceGroup `
    -ResourceName MaintenanceInstance `
    -ResourceType hosts `
    -ResourceParentName MaintenanceGroup `
    -ResourceParentType hostGroups `
    -ProviderName Microsoft.Compute
# if available, apply the update. Else, write that there are "no availabe updates" to the log
if ($isMaintenance -ne $null)
{
New-AzApplyUpdate `
    -ResourceGroupName MaintenanceGroup `
    -ResourceName MaintenanceInstance `
    -ResourceType hosts `
    -ResourceParentName MaintenanceGroup `
    -ResourceParentType hostGroups `
    -ProviderName Microsoft.Compute
}
else {
   Write-Output 'No Updates Available'
}

 

参考资料:

Azure Functions PowerShell 开发人员指南: https://docs.microsoft.com/zh-cn/azure/azure-functions/functions-reference-powershell?tabs=portal#dependency-management

Get-AzMaintenanceUpdate: https://docs.microsoft.com/en-us/powershell/module/az.maintenance/get-azmaintenanceupdate?view=azps-5.9.0

相关文章
|
21天前
|
前端开发 安全 开发工具
【11】flutter进行了聊天页面的开发-增加了即时通讯聊天的整体页面和组件-切换-朋友-陌生人-vip开通详细页面-即时通讯sdk准备-直播sdk准备-即时通讯有无UI集成的区别介绍-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
【11】flutter进行了聊天页面的开发-增加了即时通讯聊天的整体页面和组件-切换-朋友-陌生人-vip开通详细页面-即时通讯sdk准备-直播sdk准备-即时通讯有无UI集成的区别介绍-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
159 90
【11】flutter进行了聊天页面的开发-增加了即时通讯聊天的整体页面和组件-切换-朋友-陌生人-vip开通详细页面-即时通讯sdk准备-直播sdk准备-即时通讯有无UI集成的区别介绍-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
|
29天前
|
C#
【Azure Function】Function App出现System.IO.FileNotFoundException异常
Exception while executing function: xxxxxxx,The type initializer for 'xxxxxx.Storage.Adls2.StoreDataLakeGen2Reading' threw an exception. Could not load file or assembly 'Microsoft.Extensions.Configuration, Version=9.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the
115 64
|
1月前
|
JSON C# 数据格式
【Azure Function】C#独立工作模式下参数类型 ServiceBusReceivedMessage 无法正常工作
Cannot convert input parameter 'message' to type 'Azure.Messaging.ServiceBus.ServiceBusReceivedMessage' from type 'System.String'.
115 73
|
14天前
|
JavaScript API
【Azure Function】Function App门户上的Test/Run返回错误:Failed to fetch
Running your function in portal requires the app to explicitly accept requests from https://portal.azure.cn. This is known as cross-origin resource sharing (CORS).Configure CORS to add https://portal.azure.cn to allowed origins.
|
1月前
|
Java Windows
【Azure Function】部署Java Function失败:报错deploy [ERROR] Status code 401和警告 'China North 3' may not be a valid region
1:deploy [ERROR] Status code 401, (empty body). 2: China North 3 may not be a valid region,please refer to https://aka.ms/maven_function_configuration#supported-regions for values. 3:  <azure.functions.maven.plugin.version>1.36.0</azure.functions.maven.plugin.version>
43 11
|
3月前
|
移动开发 小程序 开发工具
Demo发布- ClkLog客户端集成 uni-app
在上一期推文中,我们与大家分享了 React Native 的集成 demo。本期,我们将继续介绍 ClkLog 集成 uni-app 的 demo。 uni-app 允许开发者编写一套代码,然后可以编译到 iOS、Android、H5 以及各种小程序等多个平台。因此,本次 demo 中将涵盖上述所有平台,并且我们会详细说明集成过程中遇到的难点及解决方案。
|
3月前
【Azure App Service】PowerShell脚本批量添加IP地址到Web App允许访问IP列表中
Web App取消公网访问后,只允许特定IP能访问Web App。需要写一下段PowerShell脚本,批量添加IP到Web App的允许访问IP列表里!
|
3月前
|
中间件 Docker Python
【Azure Function】FTP上传了Python Function文件后,无法在门户页面加载函数的问题
通过FTP上传Python Function至Azure云后,出现函数列表无法加载的问题。经排查,发现是由于`requirements.txt`中的依赖包未被正确安装。解决方法为:在本地安装依赖包到`.python_packages/lib/site-packages`目录,再将该目录内容上传至云上的`wwwroot`目录,并重启应用。最终成功加载函数列表。
|
4月前
|
Linux Python
【Azure Function】Python Function部署到Azure后报错No module named '_cffi_backend'
ERROR: Error: No module named '_cffi_backend', Cannot find module. Please check the requirements.txt file for the missing module.
115 2

热门文章

最新文章