zoukankan      html  css  js  c++  java
  • 【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:homesitewwwroot<your Function Name>modulesAz.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

    当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!

  • 相关阅读:
    DOM 与BOM
    尝试json文件导入数据
    js事件监听简介
    js事件简介
    js中的for语句简介
    作业练习正则表达式
    简单总结-BOM
    web前端第三次作业em,fr,rem,px简单解释及颜色表
    web第二次作业练习grid
    web前端课程第一次作业----注册页面代码(2018-9-14)
  • 原文地址:https://www.cnblogs.com/lulight/p/14761692.html
Copyright © 2011-2022 走看看