Andrew
Andrew

Reputation: 144

How to store json file content(Secret Key) on Azure key-vault secrets, using powerShell?

The json file may have one or more blocks of data as shown below.

[
    {
        "ApplicationName":  "123456.Abcv.WFcvsdfa",
        "SecretKey":  "NDRkNDJjOTAtZjBhYi00NctYjkwYTNlMjNmZTQy"
    }
]

OR

[
    {
        "ApplicationName":  "753456.Abcv.WFcvsdfa",
        "SecretKey":  "NDRkNDJjOTAtZjBhYi00NctYjkwYTNlMjNmZTQy"
    },
    {
        "ApplicationName":  "9045647.GQERDF.GDEAGJWSasdf",
        "SecretKey":  "OTlmMmMxYjgtZjEzMS00MTkwLWI1NDQtYjI2MT"
    }
    {
        "ApplicationName":  "1523456.Abcv.WFcvsdfa",
        "SecretKey":  "NDRkNDJjOTAtZjBhYi00NctYjkwYTNlMjNmZTQy"
    },
    {
        "ApplicationName":  "3645647.GQERDF.GDEAGJWSasdf",
        "SecretKey":  "OTlmMmMxYjgtZjEzMS00MTkwLWI1NDQtYjI2MT"
    }
    {
        "ApplicationName":  "5423456.Abcv.WFcvsdfa",
        "SecretKey":  "NDRkNDJjOTAtZjBhYi00NctYjkwYTNlMjNmZTQy"
    },
    {
        "ApplicationName":  "6445647.GQERDF.GDEAGJWSasdf",
        "SecretKey":  "OTlmMmMxYjgtZjEzMS00MTkwLWI1NDQtYjI2MT"
    }
]

The secrets name should be ApplicationName. Dots must be removed from the application name.

Thanks, appreciate a lots your help

enter image description here

Upvotes: 0

Views: 1818

Answers (1)

Andrew
Andrew

Reputation: 144

Here I got the solution for this post, may it will be helpful for others in future.

$VaultName = "test900066"
$hashtable = Get-Content 'out.json' | ConvertFrom-Json 

foreach ($h in $hashtable.GetEnumerator()) {
    
    $SecureString = ConvertTo-SecureString $h.SecretKey -AsPlainText -Force
    $ApplicationName = $h.ApplicationName -Replace '\.',''
    # Write-Host "$($ApplicationName) : $SecureString"
    Set-AzKeyVaultSecret -VaultName $VaultName -Name $ApplicationName -SecretValue $SecureString
}

Upvotes: 1

Related Questions