我需要在 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)。
然后在你的函数代码中,直接使用Az
命令就可以了。
示例:
$a = Get-AzWebApp -ResourceGroupName joyRG
Write-Host $a.Name
关于azure-functions - 如何在 Azure Functions 3.x PowerShell 7 中调用 Azure PowerShell 模块命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65311813/