Last active
December 11, 2023 13:54
-
-
Save techdecline/dc3251869346ff72aec140b2659fd0ea to your computer and use it in GitHub Desktop.
Sync-KeyVaultSecret.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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