如何检查 Powershell cmdlet Get-AzKeyVaultSecret 是否支持 -AsPlainText 参数?

How to check if the Powershell cmdlet Get-AzKeyVaultSecret supports -AsPlainText parameter?

使用以下命令,我可以检索 Get-AzKeyVaultSecret cmdlet 支持的参数列表(或者它是字典吗?不幸的是,我的 Powershell 知识非常有限):

PS C:\> $params = (Get-Command Get-AzKeyVaultSecret).ParameterSets | Select -ExpandProperty Parameters

PS C:\> $params | ForEach {$_.Name}
VaultName
Name
InRemovedState
DefaultProfile
Verbose
Debug
ErrorAction
WarningAction
InformationAction
...

请问我如何检查列表是否包含新版本的 cmdlet 中添加的 AsPlainText 参数?

在我的自定义脚本中,我想检查一下,然后调整我从密钥库中检索秘密值的方式:

if ($is_AsPlainText_Supported) # how to set this variable?
{
    $mySecret = Get-AzKeyVaultSecret -VaultName 'MyKeyVault' -Name 'MySecret' -AsPlainText
} 
else
{
    $mySecret = (Get-AzKeyVaultSecret -VaultName 'MyKeyVault' -Name 'MySecret').SecretValueText
}

我不想在这里使用 try/catch(或检查检索到的秘密值是否为 null),因为我的真实脚本中有很多 Get-AzKeyVaultSecret 调用,这样的方法会性价比。

在您的 if 语句之前添加此行:

$is_AsPlainText_Supported = (Get-Command Get-AzKeyVaultSecret).ParameterSets.Parameters.Name -contains "AsPlainText"

-contains 运算符将 return 一个布尔值,基于运算符之前的列表是否包含它之后的项目。