how to store primary key into kv

Welcome Forums General PowerShell Q&A how to store primary key into kv

This topic contains 6 replies, has 2 voices, and was last updated by

 
Participant
1 month ago.

  • Author
    Posts
  • #177550

    Participant
    Topics: 2
    Replies: 4
    Points: 30
    Rank: Member

    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

  • #177556

    Moderator
    Topics: 0
    Replies: 36
    Points: 339
    Team MemberHelping Hand
    Rank: Contributor

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

  • #177565

    Participant
    Topics: 2
    Replies: 4
    Points: 30
    Rank: Member

    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 🙂

  • #177580

    Moderator
    Topics: 0
    Replies: 36
    Points: 339
    Team MemberHelping Hand
    Rank: Contributor

    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!

  • #177634

    Participant
    Topics: 2
    Replies: 4
    Points: 30
    Rank: Member

    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.

  • #177640

    Moderator
    Topics: 0
    Replies: 36
    Points: 339
    Team MemberHelping Hand
    Rank: Contributor

    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.

  • #177649

    Participant
    Topics: 2
    Replies: 4
    Points: 30
    Rank: Member

    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

    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.

    Genius! Thank you!! works perfectly! 😀 thanks again!

You must be logged in to reply to this topic.