Internet Sellout

Demand Unearned Rewards

Copy Azure Vault Secrets with PowerShell

I am by no means good at PowerShell and I know I will need this info again. I wanted to copy the secrets from one Vault (Dev) to another Vault (Stage) because most are going to be the same but a couple are different. I love clicking around in the Azure portal but this is the kind of thing prone to typo, especially since the Vault UI is one of the less fun.

I found this code here:

https://stackoverflow.com/questions/55617951/how-do-i-copy-over-all-secrets-from-one-azure-keyvault-to-another-using-powershe/55618194#55618194

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
}

But now I have a few issues:

running scripts is disabled on this system 

Then, I unwisely but successfully ran this in Developer PowerShell for VS 2019 as an Administrator:

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted

Now this:

Connect-AzAccount : The term 'Connect-AzAccount' is not recognized as the name of a cmdlet, function, script file, or
operable program

I proceeded to learn that I needed an updated Nuget. I tried but I needed to do this:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

and then this:

Install-PackageProvider -Name NuGet

then this:

Install-Module Az -AllowClobber

then I ran the script and it worked.

 

Comments are closed