Search code examples
scriptingcommandazure-powershellazure-keyvaultmigrate

az commands to migrate secrets from 01 keyvault to 02 keyvault in azure


I need a script or az command to move my secrets from keyvault01 to keyvault02.

Does someone have az commands for that scenario?


Solution

  • Do you already have another Key Vault, or just want to move one to a new resource group or subscription. You can do that in the portal:

    enter image description here

    If you just need to move a few secrets you can do something like this using the cross-platform Azure module for PowerShell:

    Get-AzKeyVaultSecret -VaultName MyOldVaultName -Name test* `
      | foreach { Get-AzKeyVaultSecret -VaultName MyOldVaultName -Name $_.Name } `
      | foreach { `
        Set-AzKeyVaultSecret -VaultName MyNewVaultName `
          -Name $_.Name `
          -SecretValue $_.SecretValue `
          -Expires $_.Expires `
          -Disable:(!$_.Enabled) `
          -ContentType $_.ContentType `
          -Tag $_.Tags }
    

    You need to call Get-AzKeyVaultSecret twice because listing secrets does not download the secret value, so a second call with the specific secret value you want is necessary to retrieve it.