Forum Discussion

Rambo363636's avatar
Rambo363636
Brass Contributor
Aug 20, 2024
Solved

Powershell error with the below code

Hi Champions,

Can someone please help me with the below powershell code which i want to upload the key vault secrets excel csv file to azure key vault. But it is giving below error.

$keyVaultName = "Test-marketing"
$data = Import-Csv "C:\Users\Ram.Muppaneni\Downloads\marketing.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.Secretvalue

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

Getting below error:
Line |
6 | … -VaultName $keyVaultName -Name $secretName -SecretValue $secretValue
| ~~~~~~~~~~~~
| Cannot bind argument to parameter 'SecretValue' because it is null.

Excel csv file columns and sample data are below three
Name          Secretvalue            Type

Marketingpassword1kitchen refrigerator password 


I would like to add the above column "Type" as well to the code which needs to upload the data to the keyvault along with the Name and Secretvalue.

Please assist. 

Looking forward to hear from you.

Cheers,
Ram

  • Rambo363636 

     

    Replace the following existing line:

    $secretName = $row.$secretName

     

    With:

    $secretName = ConvertTo-SecureString -AsPlainText -String $row.$secretName -Force;

     

    The error's telling you that Set-AzKeyValutSecret wants a secure string, not a plain text string.

     

    Cheers,

    Lain

17 Replies

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    Rambo363636 

     

    Hi, Ram.

     

    The error's pretty straightforward. You just need to find which row is the issue - assuming it's not a missing or badly-formatted header from the CSV.

     

    Your CSV file should look like this:

     

    "Name","Secretvalue","Type"
    "Marketing","password1","kitchen refrigerator password"

     

    You can omit the double quotes, however, if you do, then things can begin to fail where the actual data itself contains commas. So, if the Name column contained the following value within the CSV file without surrounding double quotes:

     

    My,Value

     

    Then the Name attribute would equal "My" and the SecretValue would equal "Value".

     

    Taking that one step further, if you end up with two commas in a row and double quotes are not used, then you can end up with a null. Here's an example which would lead to SecretValue being null:

     

    Name,Secretvalue,Type
    Marketing,,password1,kitchen refrigerator password

     

    And of course, if you don't actually have a header row in the CSV file, then every row will be null for SecretValue.

     

    Here's a small statement based on your existing sample for checking which row(s) are an issue.

     

    foreach ($row in $data) {
        if ([string]::IsNullOrWhiteSpace($row.Secretvalue))
        {
            [PSCustomObject] @{
                status = "invalid";
                rowName = $row.Name;
            }
        }
        
    }

     

    Cheers,

    Lain

    • Rambo363636's avatar
      Rambo363636
      Brass Contributor

      LainRobertson 

      Hi Lain,

       

      Thank you for looking into it. I have tried editing the excel csv as advised and running the code but still getting the same error.

      $keyVaultName = "marketing-test"
      $data = Import-Csv "C:\Users\Ram.Muppaneni\Downloads\marketing.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.Secretvalue

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

      foreach ($row in $data) {
      if ([string]::IsNullOrWhiteSpace($row.Secretvalue))
      {
      [PSCustomObject] @{
      status = "invalid";
      rowName = $row.Name;
      }
      }

      }



      Tried editing the excel csv as advised and running the code but still getting below error.
      6 | … t-AzKeyVaultSecret -VaultName $keyVaultName -Name $secretName -Secret …
      | ~~~~~~~~~~~
      | Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
      Set-AzKeyVaultSecret:

      Please assistist. Thank you

      Regards,

      Ram

       

      • LainRobertson's avatar
        LainRobertson
        Silver Contributor

        Rambo363636 

         

        What I typed out above is not intended for Excel. It's intended for a plain text editor like Notepad.

         

        If you're using Excel, just type out the values as you would normally. Do not include commas or double quotes.

         

        Then, run that statement from above to check the data you imported into your $data variable.

         

        Do not use the statement I provided above in your final solution. It's only purpose is to validate the SecretValue column.

         

        Cheers,

        Lain

Resources