You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Function无法导入PowerShell模块问题求助

Azure PowerShell Function App 模块导入失败排查方案

该PowerShell Function App数日前运行正常,现突然出现Az模块相关cmdlet无法识别的错误,具体报错及配置文件如下:

相关配置文件

host.json

{
    "version": "2.0",
    "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[3.3.0, 4.0.0)"
    }
}

Requirements.psd1

# This file enables modules to be automatically managed by the Functions service.
# See https://aka.ms/functionsmanageddependency for additional information.
#
@{
    # For latest supported version, go to 'https://www.powershellgallery.com/packages/Az'. 
    # To use the Az module in your function app, please uncomment the line below.
     'Az' = '7.1.0'
     'Az.Accounts' = '2.2.3'
     'Az.Storage' = '5.10.1'
     'Az.Keyvault' = '3.3.0'
}

Profile.ps1

# Azure Functions profile.ps1
#
# This profile.ps1 will get executed every "cold start" of your Function App.
# "cold start" occurs when:
#
# * A Function App starts up for the very first time
# * A Function App starts up after being de-allocated due to inactivity
#
# You can define helper functions, run commands, or specify environment variables
# NOTE: any variables defined that are not environment variables will get reset after the first execution

# Authenticate with Azure PowerShell using MSI.
# Remove this if you are not planning on using MSI or Azure PowerShell.
if ($env:MSI_SECRET) {
    Disable-AzContextAutosave -Scope Process | Out-Null
    Connect-AzAccount -Identity
}

Import-Module Az -RequiredVersion "7.1.0"
Import-Module Az.Keyvault -RequiredVersion "3.3.0"
Import-Module Az.Accounts -RequiredVersion "2.2.3"
Import-Module Az.Storage -RquiredVersion "5.10.1"


# Uncomment the next line to enable legacy AzureRm alias in Azure PowerShell.
# Enable-AzureRmAlias

# You can also define functions or aliases that can be referenced in any of your PowerShell functions.

报错信息

[Warning] The Function app may be missing a module containing the 'Disable-AzContextAutosave' command definition

[Warning] The Function app may be missing a module containing the 'Connect-AzAccount' command definition.

[Error] ERROR: The term 'Disable-AzContextAutosave' is not recognized as a name of a cmdlet, function, script file, or executable program.

[Warning] The Function app may be missing a module containing the 'Get-AzKeyVaultSecret' command definition.

排查步骤

  1. 修正Profile.ps1拼写错误
    Profile.ps1中导入Az.Storage模块时,参数-RquiredVersion拼写错误,应为-RequiredVersion。该错误会导致Az.Storage模块无法正常导入,甚至影响其他模块加载逻辑。

  2. 调整模块导入与认证逻辑顺序
    当前代码在调用Disable-AzContextAutosave、Connect-AzAccount后才导入模块,但这两个cmdlet属于Az.Accounts模块,必须先导入模块才能调用。修改顺序如下:

    # 先导入所需模块
    Import-Module Az.Accounts -RequiredVersion "2.2.3"
    Import-Module Az.Keyvault -RequiredVersion "3.3.0"
    Import-Module Az.Storage -RequiredVersion "5.10.1"
    Import-Module Az -RequiredVersion "7.1.0"
    
    # 再执行MSI认证逻辑
    if ($env:MSI_SECRET) {
        Disable-AzContextAutosave -Scope Process | Out-Null
        Connect-AzAccount -Identity
    }
    
  3. 检查托管依赖安装状态

    • 进入Function App门户页面,导航到配置 > 功能 > 托管依赖项,确认状态为启用,且Az系列模块已成功安装。
    • 若安装失败,尝试重启Function App,或删除requirements.psd1后重新上传,触发依赖重新安装。
  4. 修复模块版本兼容性问题
    Az 7.1.0与指定的Az.Accounts 2.2.3版本存在兼容性差异,建议简化requirements.psd1,只保留Az主模块版本,让系统自动匹配兼容的子模块:

    @{
        'Az' = '7.1.0'
    }
    
  5. 验证运行时版本支持
    Az 7.x系列需要PowerShell 7.0及以上版本,在门户配置 > 常规设置中确认Function App运行时版本为PowerShell 7.x,若版本过低则调整升级。

内容的提问来源于stack exchange,提问作者Jacob King

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 12:35:31