# Trying to update the KeyVault Secrets Tag from CSV

**URL:** <https://forums.powershell.org/t/trying-to-update-the-keyvault-secrets-tag-from-csv/22376>\
**Category:** PowerShell Help\
**Created:** [May 27, 2023, 4:03am UTC](https://forums.powershell.org/t/trying-to-update-the-keyvault-secrets-tag-from-csv/22376 "2023-05-27T04:03:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bshwjt](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bshwjt/32/4581_2.png) [@bshwjt](https://forums.powershell.org/u/bshwjt)\
**Post date:** [May 27, 2023, 4:03am UTC](https://forums.powershell.org/t/trying-to-update-the-keyvault-secrets-tag-from-csv/22376/1 "2023-05-27T04:03:05Z")

</div>

Trying to update the KeyVault Secrets Tag. All is good except for the tag. The tag is not getting updated.

I tried with the below code & CSV.

```auto
$keyVaultName = "test-kv01"
$data = Import-Csv "C:\temp\kv.csv"

# Iterate over each row in the CSV and create/update the secrets in the Key Vault
foreach ($row in $data) {
    $secretName = $row.Name
    $secretValue = $row.Secret
    $secretTags = ConvertFrom-Json -InputObject $row.Tags

    # Create or update the secret
    Set-AzKeyVaultSecret -VaultName $keyVaultName -Name $secretName -SecretValue $secretValue -Tag $secretTags
}

<# Sample CSV
Name,Secret,Tags
secret1,secret01,@{ 'Severity' = 'medium'; 'IT' = 'true'}
secert2,secret02,@{ 'Severity' = 'medium'; 'IT' = 'true'}

#>

```

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [May 27, 2023, 8:07am UTC](https://forums.powershell.org/t/trying-to-update-the-keyvault-secrets-tag-from-csv/22376/2 "2023-05-27T08:07:52Z")

</div>

Hi, welcome back 👋

The `-Tag` parameter expects a hashtable, but by default `ConvertFrom-Json` outputs a `PSCustomObject`.

If you’re using PowerShell 7.x then `ConvertFrom-Json` has an `-AsHashTable` parameter, otherwise you’ll have to build it yourself:

```auto
$json = @"
{
    "Name": "Bob",
    "Country": "United Kingdom"
}
"@

$obj = $json | ConvertFrom-Json

$hashtable = @{}

foreach ($key in $obj.PSObject.Properties) {
    $hashtable.Add($key.Name,$key.Value)
}

```

---

<div class="post-metadata">

**Author:** ![bshwjt](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bshwjt/32/4581_2.png) [@bshwjt](https://forums.powershell.org/u/bshwjt)\
**Post date:** [May 27, 2023, 10:03am UTC](https://forums.powershell.org/t/trying-to-update-the-keyvault-secrets-tag-from-csv/22376/3 "2023-05-27T10:03:33Z")

</div>

Thank you 🙂 . I also fixed that in another way.

```auto
$keyVaultName = "KeyVault01"
$data = Import-Csv "C:\temp\test.csv"
# Iterate over each row in the CSV and create/update the secrets in the Key Vault
foreach ($row in $data) {
    Try {
    $secretName = $row.Name
    $secretValue = convertto-securestring -string $row.Secret -asplaintext -force
    $TagName =$row.TagName
    $TagValue = $row.TagValue
    # Create or update the secret
    Set-AzKeyVaultSecret -VaultName $keyVaultName -Name $secretName -SecretValue $secretValue -Tag @{"$TagName"="$TagValue"}
}
catch
{
Write-Output "Error: $($_.Exception.Message)"
}

}
<#
Name,Secret,TagName,TagValue
secret1,secret01,Biswajit,IT
secert2,secret02,Siva,IT
#>

```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:14pm UTC](https://forums.powershell.org/t/trying-to-update-the-keyvault-secrets-tag-from-csv/22376/4 "2024-05-16T20:14:52Z")

</div>


