azure-functions - 如何在 Azure Functions 3.x PowerShell 7 中调用 Azure PowerShell 模块命令?

标签 azure-functions

我需要在 Azure Functions 3 中运行 Azure PowerShell 模块命令 ( https://learn.microsoft.com/en-us/powershell/azure/install-az-ps?view=azps-5.2.0) 脚本。

我不想在每次函数调用时都运行 Install-Module。我希望有更好的方法来做到这一点。 Azure PowerShell 模块相当大。

我正在阅读以下文档,但找不到有关如何调用 Azure PowerShell 模块命令的任何引用资料L https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-powershell?tabs=portal

如何在 Azure Functions 3.x PowerShell 7 中调用 Azure PowerShell 模块命令?

最佳答案

在这种情况下无需使用Install-Module,目前,当您在门户中创建powershell 函数时,它会默认为您安装Az 模块通过 Dependency management功能。

您可以检查门户中的 App files 边栏选项卡以确保您的函数应用配置正确,如果不正确,请按如下所示进行更改。

host.json

{
  "version": "2.0",
  "managedDependency": {
    "Enabled": true
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[1.*, 2.0.0)"
  }
}

requirements.psd1

@{
    # For latest supported version, go to 'https://www.powershellgallery.com/packages/Az'. 
    'Az' = '5.*'
}

profile.ps1

if ($env:MSI_SECRET) {
    Disable-AzContextAutosave -Scope Process | Out-Null
    Connect-AzAccount -Identity
}

通过以上设置,函数应用会自动为你安装Az模块,并使用MSI(managed identity)登录Az模块。你的函数应用程序(profile.ps1 中的命令完成了),很方便。

要在函数中使用 Az 命令,您只需为您的函数应用启用 MSI 和 assign the RBAC role to your MSI (取决于具体情况,例如,如果您想列出订阅/资源组中的所有 Web 应用程序,您需要在订阅/资源组范围内将 Reader 之类的角色授予您的 MSI)。

enter image description here

然后在你的函数代码中,直接使用Az命令就可以了。

示例:

$a = Get-AzWebApp -ResourceGroupName joyRG
Write-Host $a.Name

enter image description here

关于azure-functions - 如何在 Azure Functions 3.x PowerShell 7 中调用 Azure PowerShell 模块命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65311813/

相关文章:

c# - 从 azure 函数连接到 Azure 数据湖 Gen 2

c# - 将图像列表发送到 azure 函数以将图像上传到 .Net 中的 blob

powershell - 验证多个 Azure Powershell 函数

Azure函数: Azure Functions Core Tools and Zip Deployment confusion

Azure Functions C# 黄色圆圈感叹号且没有日志

azure - MailChimp API Webhook header

azure - Azure Function App 中的文件写入权限

c# - 保护 Azure Functions 使用的私钥

python - 如何使用python SDK获取Azure FunctionApp列表

c# - 有没有办法更改 azure 函数的 blob 绑定(bind)中的 blob 路径?