how to store primary key into kv

Hi

 

I have used the below commands to retrieve primary key from redis cache but how can i store the primary key into the keyvault in the resource group?

 

Login-AzAccount
Select-AzSubscription -SubscriptionId xxx.xx.xx
Get-AzResourceGroup -ResourceGroupName test
Get-AzRedisCacheKey -ResourceGroupName test -Name test01cache

 

thanks

Personally, I’d advise having a look at `Set-AzKeyVaultSecret`. Can we assume the vault exists, already?

Hi James

Yes the keyvault already exists, I am fairly new to powershell so am quite stuck after the commands above it displays the primary key but not sure what powershell command to use to use that primary key and store it as a secret in a keyvault…

 

Thanks for your help :slight_smile:

OK then!

 $ResourceGroupName = "test"

# We retrieve the key from the 'test01cache' Redis cache, and store it in a variable ('CacheKey')
$CacheKey = Get-AzRedisCacheKey -ResourceGroupName $ResourceGroupName -Name "test01cache"

# We find the KeyVault in the resource group (assuming there's only one)
$KeyVault = Get-AzKeyVault -ResourceGroupName $ResourceGroupName

# Set-AzKeyVaultSecret accepts SecureString, not string, so we probably need to convert the key
$SecureCacheKey = ConvertTo-SecureString -String $CacheKey -AsPlainText -Force

# We can then store $SecureCacheKey in the KeyVault we found
Set-AzKeyVaultSecret -VaultName $KeyVault.VaultName -Name "RedisCacheKey" -SecretValue $SecureCacheKey

Hope that helps!

Hi James.

 

Thank you so much for that, it works but… its not storing the primary key though…

When I go to KV and look for the RedisCacheKey entry the secret value is showing as “Microsoft.Azure.Management.Redis.Models.RedisAccessKeys”

 

Its not displaying the primary key from the rediscache…

 

Thank you again for helping.

Ah, apologies. This is where I regret not testing the code / having a Redis Cache to test against.

It seems likely that there will be a property on that object that we need to specify, instead of using the default ToString method. Can you, excluding any potentially sensitive information, show the output of either:

$CacheKey

# ...or...

$CacheKey | Get-Member

Edit: A quick Google shows that it should have two properties - PrimaryKey and SecondaryKey.

If you adjust the code above to `ConvertTo-SecureString $CacheKey.PrimaryKey […]`, that should solve it.

[quote quote=177640]Ah, apologies. This is where I regret not testing the code / having a Redis Cache to test against.

It seems likely that there will be a property on that object that we need to specify, instead of using the default ToString method. Can you, excluding any potentially sensitive information, show the output of either:

PowerShell
5 lines
<textarea class="ace_text-input" style="opacity: 0; height: 17.6px; width: 6.59775px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
4
5
$CacheKey
# ...or...
$CacheKey | Get-Member
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Edit: A quick Google shows that it should have two properties – PrimaryKey and SecondaryKey.

If you adjust the code above to `ConvertTo-SecureString $CacheKey.PrimaryKey […]`, that should solve it.

[/quote]

Genius! Thank you!! works perfectly! :smiley: thanks again!