Azure Function App PowerShell Function中Get-AzAccessToken返回值类型从System.String改为System.Security.SecureString,有何深意?

摘要:问题描述 把PowerShell Function部署到Azure Function App后,发现在不同的环境中,使用Get-AzAccessToken函数得到的返回值类型发生了变化。 正常情况为 System.
问题描述 把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.