Skip to content

Instantly share code, notes, and snippets.

@cajuncoding
Last active August 12, 2022 02:30
Show Gist options
  • Save cajuncoding/085527fdc5954b239dd72bff0e0951e4 to your computer and use it in GitHub Desktop.
Save cajuncoding/085527fdc5954b239dd72bff0e0951e4 to your computer and use it in GitHub Desktop.
Simple PowerShell Scripts for: Copying KeyVault Secrets between two Key Vaults & Deleting All KeyVault Secrets
#**************************************************************************************************
#COPY All Secrets from Source KeyVault to Destination (with ContentType)
#Originally Inspired by the StackOverflow post here: https://stackoverflow.com/a/55618194/7293142
# To Use: https://docs.microsoft.com/en-us/powershell/azure/install-az-ps
# 1) Ensure you have permission to execute in PowerShell:
# Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# 2) Install Azure "Az" Modeule:
# Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force
#**************************************************************************************************
Param(
[Parameter(Mandatory)] [string] $sourceVaultName,
[Parameter(Mandatory)] [string] $destVaultName
)
Connect-AzAccount
$secretNames = (Get-AzKeyVaultSecret -VaultName $sourceVaultName).Name
$secretNames.foreach{
$secret = (Get-AzKeyVaultSecret -VaultName $sourceVaultName -Name $_)
Write-Host("Copying Secret ["+$secret.Name+"]...")
Set-AzKeyVaultSecret -VaultName $destVaultName -Name $secret.Name -ContentType $secret.ContentType -SecretValue $secret.SecretValue
}
#**************************************
#DELETE ALL Secrets from KeyVault
#**************************************
Param(
[Parameter(Mandatory)]
[string]$vaultName
)
$secretItems = (Get-AzKeyVaultSecret -VaultName $vaultName)
$secretItems.foreach{
#Write-Host("Name=[" + $_.Name + "] ContentType=[" + $_.ContentType + "]")
Write-Host("DELETING [" + $_.Name + "]")
Remove-AzKeyVaultSecret -VaultName $vaultName -Name $_.Name -Force
#Remove-AzKeyVaultSecret -VaultName $vaultName -Name $_.Name -Force -InRemovedState
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment