警告:有关破坏cmdlet'Get-AzKeyVaultSecret'中的更改的内容SecretValueText不推荐使用Az4.6.1

问题描述

我今天将Az Powershell升级到4.6.1,并开始看到以下警告。我的问题是我应该如何处理此警告?我可以忽略警告,但这根本无法帮助我为这一重大变化做准备。我检查了Az 4.6.1 Microsoft docs,他们告诉我我仍然应该使用SecretValueText,并且不提供有关弃用或获取秘密值的任何其他方式的类似警告。那么,用于使用SecretValueText读取keyvault机密的Powershell的更新路径是什么?

WARNING: Breaking changes in the cmdlet 'Get-AzkeyvaultSecret' :
WARNING:  - "The output type 'Microsoft.Azure.Commands.keyvault.Models.PSkeyvaultSecret' is changing" 
- The following properties in the output type are being deprecated :
 'SecretValueText'
WARNING: Note :The change is expected to take effect from the version :  '3.0.0'
WARNING:  - "The output type 'Microsoft.Azure.Commands.keyvault.Models.PSDeletedkeyvaultSecret' is changing"
 - The following properties in the output type are being deprecated :
 'SecretValueText'
WARNING: Note :The change is expected to take effect from the version :  '3.0.0'
WARNING: NOTE : Go to https://aka.ms/azps-changewarnings for steps to suppress this breaking change warning,and other @R_92_4045@ion on breaking changes in Azure PowerShell.

这是Microsoft docs中的当前示例:

$secret = Get-AzkeyvaultSecret -VaultName 'Contoso' -Name 'ITSecret'
Write-Host "Secret Value is:" $secret.SecretValueText

Secret Value is: P@ssw0rd

解决方法

这可以通过以下方式完成:

通过以下方式获取秘密:

$secret = Get-AzKeyVaultSecret -VaultName {YourVaultName} -Name {YourSecret}
$pass = $secret.SecretValue | ConvertFrom-SecureString -AsPlainText

这与 $ secret.SecretValueText

,

好吧,即使SecretValueText被弃用,也有一种方法将始终有效。

只需使用$secret.SecretValue,它就是一个System.Security.SecureString,我们只需要将其转换为String,下面的$Password就可以了。

$secret = Get-AzKeyVaultSecret -VaultName joykeyvault -Name mySecret123
$SecurePassword = $secret.SecretValue
$Password = [System.Net.NetworkCredential]::new("",$SecurePassword).Password

enter image description here

,

PowerShell 7中支持ConvertFrom-SecureString -AsPlainText。请勿在较低版本上尝试

,

Microsoft文档现已更新 本示例摘自最新的docs

$secret = Get-AzKeyVaultSecret -VaultName 'Contoso' -Name 'ITSecret'
$secretValueText = '';
$ssPtr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secret.SecretValue)
try {
    $secretValueText = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ssPtr)
} finally {
    [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ssPtr)
}
Write-Host "Secret Value is:" $secretValueText

Secret Value is: P@ssw0rd
,

您可以在 Get-AzKeyVaultSecret 上使用 q <- ggplot() + ... q + scale_x_discrete(labels = function(x) str_wrap(x,width = 8)) 开关。

-AsPlainText

另一种选择是将 $secretText = Get-AzKeyVaultSecret -VaultName 'Contoso' -Name 'ITSecret' -AsPlainText 属性添加回 SecretValueText 对象。

Microsoft.Azure.Commands.KeyVault.Models.PSKeyVaultSecretIdentityItem