错误 Get-AzureKeyVaultSecret:无法检索 ServicePrincipal 帐户的服务密钥

Error Get-AzureKeyVaultSecret : Unable to retrieve service key for ServicePrincipal account

使用 PowerShell,我可以在本地访问我的 Azure KeyVault,提供机密和 return 存储的密码。我也可以从我启动的 VM 中执行此操作。此外,我可以从 KeyVault 访问单个密码并使用它来模拟 VSTS 构建代理上的用户,但我无法访问其余存储的密码。给出了什么?

这是我收到的消息:

[error]Get-AzureKeyVaultSecret : Unable to retrieve service key for ServicePrincipal account [hiddenaccountname]@[companyaccount].com. Please log in again to supply the credentials for this service principal. In PowerShell, execute Login-AzureRMAccount for Azure Resource Manager cmdlets or Add-AzureAccount for service management cmdlets.

...

  • CategoryInfo : CloseError: (:) [Get-AzureKeyVaultSecret], KeyNotFoundException
  • FullyQualifiedErrorId : Microsoft.Azure.Commands.KeyVault.GetAzureKeyVaultSecret

这是我的代码在这一部分的样子:

Login-AzureRmAccount -Credential $AzureCredential -ServicePrincipal -TenantId [abunchofnumbers]

$Password  = (Get-AzureKeyVaultSecret -VaultName "[nameOfVault]" -Name "[nameOfSecret]").SecretValueText

登录接受提供的凭据并登录,但 Get-AzureKeyVaultSecret cmdlet 失败,并且仅在构建期间在 VSTS 上失败。

有什么想法吗,你们这些奇怪而出色的开发者?

[已解决 - 跟进] 我的过程涉及一个 Powershell 脚本,通过使用 Login-AzureRmAccount 和 Get-AzureKeyVaultSecret 来模拟用户来获取用户的密码,然后是第二个 Powershell 脚本来重新登录到 AzureRmAccount 以从 KeyVault 获取更多密码。我交换了这两个脚本的顺序,问题就消失了。