My Digital Garden

Copy secrets between Azure Key Vaults

Copy secrets between Azure Key Vaults

Azure CLI

# Login to Azure (Tenant A)
az login

# List and store the secrets from the source Key Vault
sourceVaultName="SourceVaultName"
secrets=$(az keyvault secret list --vault-name $sourceVaultName --query "[].name" -o tsv)

# Export the secrets (Note: This only exports secret names, not values)
for secret in $secrets; do
    value=$(az keyvault secret show --name $secret --vault-name $sourceVaultName --query "value" -o tsv)
    az keyvault secret set --vault-name "DestinationVault" --name $secret --value "$value"
done

Powershell

Single secret

Get-AzKeyVaultSecret -VaultName "SourceVault" -Name "SecretName" | Set-AzKeyVaultSecret -VaultName "DestinationVault"

All the secrets

Param(
    [Parameter(Mandatory)]
    [string]$sourceVaultName,
    [Parameter(Mandatory)]
    [string]$destVaultName
)

Connect-AzAccount

$secretNames = (Get-AzKeyVaultSecret -VaultName $sourceVaultName).Name
$secretNames.foreach{
    Set-AzKeyVaultSecret -VaultName $destVaultName -Name $_ `
        -SecretValue (Get-AzKeyVaultSecret -VaultName $sourceVaultName -Name $_).SecretValue
}

See also

internal and external references