Skip to content

Instantly share code, notes, and snippets.

@techdecline
Last active December 11, 2023 13:54
Show Gist options
  • Select an option

  • Save techdecline/dc3251869346ff72aec140b2659fd0ea to your computer and use it in GitHub Desktop.

Select an option

Save techdecline/dc3251869346ff72aec140b2659fd0ea to your computer and use it in GitHub Desktop.
Sync-KeyVaultSecret.ps1
param(
$FilterString,
$SourceSubscriptionId,
$SourceVaultName,
$DestinationVaultName,
$DestinationSubscriptionId
)
# Change Context to source Subscription
Set-AzContext -Subscription $SourceSubscriptionId
# Get all secrets from the source vault
$secrets = Get-AzKeyVaultSecret -VaultName $SourceVaultName
# Filter the secrets based on the filter string
$filteredSecrets = $secrets | Where-Object { $_.Name -like "*$filterString*" } | ForEach-Object {
Get-AzKeyVaultSecret -VaultName $SourceVaultName -Name $_.Name | Select-Object -Property Name, SecretValue, ContentType
}
# Change the context to the destination subscription
Set-AzContext -Subscription $DestinationSubscriptionId
# # Loop over the filtered secrets and set them in the destination vault
foreach ($secret in $filteredSecrets) {
Set-AzKeyVaultSecret -VaultName $DestinationVaultName -Name $secret.Name -SecretValue $secret.SecretValue -ContentType $secret.ContentType
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment