I am trying to copy secrets from one Azure Key Vault to another.
So I have written 2 Powershell functions -
One for reading secrets:
function GetSecretValue
{
param(
[String] $KeyvaultName,
[String] $SecretName
)
Write-Host "Retrieving secret $SecretName from $KeyvaultName... " -NoNewline
if ((Get-Command Get-AzKeyVaultSecret).ParameterSets.Parameters.Name -contains "AsPlainText")
{
# Newer Get-AzKeyVaultSecret version requires -AsPlainText parameter
$SecretValue = Get-AzKeyVaultSecret -VaultName $KeyvaultName -Name $SecretName -AsPlainText
}
else
{
$SecretValue = (Get-AzKeyVaultSecret -VaultName $KeyvaultName -Name $SecretName).SecretValueText
}
Write-Host "ok"
return $SecretValue
}
And another one for creating secrets:
function SetSecretValue
{
param(
[String] $KeyvaultName,
[String] $SecretName,
[String] $SecretValue
)
Write-Host "Creating secret $SecretName in $KeyvaultName... " -NoNewline
$SecureStr = ConvertTo-SecureString -String $SecretValue -AsPlainText -Force
Set-AzKeyVaultSecret -VaultName $keyvaultName -Name $secretName -SecretValue $SecureStr
Write-Host "ok"
}
They seem to work ok, but when I (a Powershell newbie) try to combine them -
SetSecretValue($DestKv, 'ClientId', GetSecretValue($SrcKv, 'ClientId'))
SetSecretValue($DestKv, 'ClientSecret', GetSecretValue($SrcKv, 'ClientSecret'))
Then I get the syntax error:
+ SetSecretValue($DestKv, 'ClientId', GetSecretValue($Src ...
+ ~
Missing closing ')' in expression.
SetSecretValue $DestKv 'ClientId' -SecretValue (GetSecretValue $srcKv 'ClientId'). You might also want to rename your functions to follow PowerShell'sVerb-Nounsyntax (eg.Set-SecretValueandGet-SecretValue)foo arg1 arg2- not like C# methods -foo('arg1', 'arg2'). If you use,to separate arguments, you'll construct an array that a command sees as a single argument. To prevent accidental use of method syntax, useSet-StrictMode -Version 2or higher, but note its other effects. See this answer for more information.