Invalid JSON primitive: Microsoft.Azure.Commands.KeyVault.Models.PSKeyVaultSecret

Vijay Kumar 2,036 Reputation points
2022-02-11T01:09:47.943+00:00

Hi Team,

I am using the below script on Windows Server 2019 to fetch details from Keyvault using PowerShell:

$User = user1@jaswant .com
$SecurePassword = Get-Content "D:\PSScripts\accessPwd.dat" | ConvertTo-SecureString
$PSCredential = New-Object System.Management.Automation.PSCredential($User,$SecurePassword)
$Password = $PSCredential.GetNetworkCredential().Password
az login -u $User -p $Password

$SASKeyJson = Get-AzKeyVaultSecret -name "secret1" -vaultname "abcxyzvault"
$SASKey = $SASKeyJson | ConvertFrom-Json
$KeyValue = $SASKey.value
$KeyValue
Set-Content -Path "D:\PSScripts\KeyValue.txt" -Value $KeyValue
$SASKeyJson = Get-AzKeyVaultSecret -name "secret2" -vaultname "abcxyzvault"
$SASKey = $SASKeyJson | ConvertFrom-Json
$KeyValue = $SASKey.value
$KeyValue
Set-Content -Path "D:\PSScripts\KeyPWD.txt" -Value $KeyValue
$SASKeyJson = Get-AzKeyVaultSecret -name "secret3" -vaultname "abcxyzvault"
$SASKey = $SASKeyJson | ConvertFrom-Json
$KeyValue = $SASKey.value
$KeyValue
Set-Content -Path "D:\PSScripts\KeyUser.txt" -Value $KeyValue

The authentication part is working fine.

But getting below error after that:

*ConvertFrom-Json : Invalid JSON primitive: Microsoft.Azure.Commands.KeyVault.Models.PSKeyVaultSecret.
At D:\PSScript\GetKey1.ps1:3 char:25

  • $SASKey = $SASKeyJson | ConvertFrom-Json
  • ~~~~~~~~~~~~~~~~
  • CategoryInfo : NotSpecified: (:) [ConvertFrom-Json], ArgumentException
  • FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

ConvertFrom-Json : Invalid JSON primitive: Microsoft.Azure.Commands.KeyVault.Models.PSKeyVaultSecret.
At D:\PSScript\GetKey1.ps1:8 char:25

  • $SASKey = $SASKeyJson | ConvertFrom-Json
  • ~~~~~~~~~~~~~~~~
  • CategoryInfo : NotSpecified: (:) [ConvertFrom-Json], ArgumentException
  • FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

Get-AzKeyVaultSecret : A parameter cannot be found that matches parameter name 'vault-name'.
At D:\PSScript\GetKey1.ps1:12 char:61

  • ... n = Get-AzKeyVaultSecret -name "SnowProdUsername" -vault-name "snf-do ...
  • ~~~~~~~~~~~
  • CategoryInfo : InvalidArgument: (:) [Get-AzKeyVaultSecret], ParameterBindingException
  • FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.Azure.Commands.KeyVault.GetAzureKeyVaultSecret

ConvertFrom-Json : Invalid JSON primitive: Microsoft.Azure.Commands.KeyVault.Models.PSKeyVaultSecret.
At D:\PSScript\GetKey1.ps1:13 char:25

  • $SASKey = $SASKeyJson | ConvertFrom-Json
  • ~~~~~~~~~~~~~~~~
  • CategoryInfo : NotSpecified: (:) [ConvertFrom-Json], ArgumentException
  • FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand*
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2022-02-11T02:52:16.687+00:00

    Take the content of the $SASKeyJson variable and paste it into this URL: jsonlint.com

    But if you're just looking to retrieve the secret, try adding the -AsPlainText switch to the cmdlet.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.