【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

相关文章
|
11月前
|
存储 安全 Linux
【Azure App Service】在App Service中查看CA证书
在 Azure App Service 中,使用自签名或私有 CA 证书的远程服务可能会导致 SSL 握手失败。解决方法包括使用受信任 CA 签发的证书,或通过 App Service Environment 加载自定义根证书,实现安全连接。
244 4
|
9月前
|
Java 应用服务中间件 API
【App Service】部署War包到Azure云上遇404错误
Java应用部署至Azure App Service for Windows后报404,本地运行正常。经排查,日志提示类文件版本不兼容:应用由Java 17(class file version 61.0)编译,但环境仅支持到Java 11(55.0)。错误根源为Java版本不匹配。调整App Service的Java版本至17后问题解决,成功访问接口。
1077 3
|
9月前
|
存储 Linux 网络安全
【Azure App Service】Root CA on App Service
Azure App Service for Windows应用连接外部SSL服务时,需确保其证书由受信任的根CA颁发。多租户环境下无法修改根证书,但ASE(单租户)可加载自定义CA证书。若遇证书信任问题,可更换为公共CA证书或将应用部署于ASE并导入私有CA证书。通过Kudu的PowerShell(Windows)或SSH(Linux)可查看当前受信任的根证书列表。
211 13
|
10月前
|
API 网络架构 容器
【Azure Container App】查看当前 Container App Environment 中的 CPU 使用情况的API
在扩展 Azure Container Apps 副本时,因 Container App Environment 的 CPU 核心数已达上限(500 cores),导致扩展失败。本文介绍如何使用 `az rest` 命令调用 Azure China Cloud 管理 API,查询当前环境的 CPU 使用情况,并提供具体操作步骤及示例。
356 17
|
10月前
|
网络协议 Java Linux
【App Service】在Azure环境中如何查看App Service实例当前的网络连接情况呢?
在 Azure App Service(Windows 和 Linux)中部署应用时,分析网络连接状态是排查异常、验证端口监听及确认后端连接的关键。本文介绍如何在 Linux 环境中使用 `netstat` 命令查看特定端口(如 443、3306、6380)的连接情况,并解析输出结果。同时说明在 Windows App Service 中 `netstat` 被禁用的情况下,如何通过门户抓包等替代方法进行网络诊断。内容涵盖命令示例、操作步骤及附录说明,帮助开发者快速掌握云环境中的网络分析技巧。
253 11
|
10月前
|
数据安全/隐私保护
【Azure Function App】PowerShell Function 执行 Get-AzAccessToken 的返回值类型问题:System.String 与 System.Security.SecureString
将PowerShell Function部署到Azure Function App后,Get-AzAccessToken返回值类型在不同环境中有差异。正常为SecureString类型,但部分情况下为System.String类型,导致后续处理出错。解决方法是在profile.ps1中设置环境变量$env:AZUREPS_OUTPUT_PLAINTEXT_AZACCESSTOKEN=false,以禁用明文输出。
250 1
|
人工智能 Python
083_类_对象_成员方法_method_函数_function_isinstance
本内容主要讲解Python中的数据类型与面向对象基础。回顾了变量类型(如字符串`str`和整型`int`)及其相互转换,探讨了加法在不同类型中的表现。通过超市商品分类比喻,引出“类型”概念,并深入解析类(class)与对象(object)的关系,例如具体橘子是橘子类的实例。还介绍了`isinstance`函数判断类型、`type`与`help`探索类型属性,以及`str`和`int`的不同方法。最终总结类是抽象类型,对象是其实例,不同类型的对象有独特运算和方法,为后续学习埋下伏笔。
315 7
083_类_对象_成员方法_method_函数_function_isinstance
|
Python
[oeasy]python086方法_method_函数_function_区别
本文详细解析了Python中方法(method)与函数(function)的区别。通过回顾列表操作如`append`,以及随机模块的使用,介绍了方法作为类的成员需要通过实例调用的特点。对比内建函数如`print`和`input`,它们无需对象即可直接调用。总结指出方法需基于对象调用且包含`self`参数,而函数独立存在无需`self`。最后提供了学习资源链接,方便进一步探索。
362 17
|
人工智能 Python
[oeasy]python083_类_对象_成员方法_method_函数_function_isinstance
本文介绍了Python中类、对象、成员方法及函数的概念。通过超市商品分类的例子,形象地解释了“类型”的概念,如整型(int)和字符串(str)是两种不同的数据类型。整型对象支持数字求和,字符串对象支持拼接。使用`isinstance`函数可以判断对象是否属于特定类型,例如判断变量是否为整型。此外,还探讨了面向对象编程(OOP)与面向过程编程的区别,并简要介绍了`type`和`help`函数的用法。最后总结指出,不同类型的对象有不同的运算和方法,如字符串有`find`和`index`方法,而整型没有。更多内容可参考文末提供的蓝桥、GitHub和Gitee链接。
372 11