Forum Discussion
Powershell error with the below code
- Aug 26, 2024
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
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
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