问题描述
把PowerShell Function部署到Azure Function App后,发现在不同的环境中,使用Get-AzAccessToken函数得到的返回值类型发生了变化。
正常情况为 System.Security.SecureString 类型(加密类型字符串),但遇见异常的时候类型为System.String(明文字符串),导致了后面获取AccessToken处理时,因类型不对而报错。
正确的类型要求如下:
如果遇见了类型显示为System.String的情况下,如何来缓解这个问题呢?
问题解答
在PowerShell Function的 profile.ps1 文件中增加 变量 $env:AZUREPS_OUTPUT_PLAINTEXT_AZACCESSTOKEN 并设置值为 false, 这样就可以是的PowerShell返回AccessToken的时候不适用纯文本(PlainText)。
操作步骤截图如下
出现这种情况的解释
Az.Accounts 模块从 5.0 开始计划引入一个破坏性变更,将默认返回值从明文(plain text)改为 SecureString。然而,在变更发布前,Azure Functions 团队评估后认为这会对现有 PowerShell Function 应用造成重大影响,因为用户在未进行任何更改的情况下就会遭遇运行中断。
为了确保 Azure Functions 应用的稳定性,Azure Function团队做出了一个特例处理:当 Get-AzAccessToken 在 Functions 环境中运行时,它将默认保留 5.0 之前的行为,返回明文。 当然,命令仍然支持 -AsSecureString 参数,该行为保持不变。
虽然锁定旧 Runtime 看似解决了问题,但这并非推荐做法,因其伴随更多潜在问题。如把Function运行时版本指定为 4.1037.1.23605,可以作为一个临时方案。
但是,Azure Function还是建议最正确的解决方案是:在profile.ps1中增加 环境变量 $env:AZUREPS_OUTPUT_PLAINTEXT_AZACCESSTOKEN 并设置为false。
参考资料
Azure PowerShell release notes : https://learn.microsoft.com/en-us/powershell/azure/release-notes-azureps?view=azps-14.4.0#1400---may-2025
Az.Accounts 5.0.0
- Changed the default output access token of 'Get-AzAccessToken' from plain text to 'SecureString'.
- Removed the warning message about failing to initialize PSStyle in automation runbooks. [#26155]
- Increased the timeout for tab-completion of location, resource group, etc. to 10 seconds.
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!