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
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
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
- oliwer_sundgrenAug 21, 2024Iron ContributorHi Rambo363636 Just like LainRobertson said when you work with CSV in Excel you dont need to specify double quotes or the delimiter (comma) in the cells itself.
When you run $data = Import-Csv "C:\Users\Ram.Muppaneni\Downloads\marketing.csv"
I would recommend that you also double check that the data is correctly formated by just typing out $Data or $data.Value and $data.Name to see that everything outputs correctly.
Sometimes Excel defaults to the incorrect delimiter that Powershell assumes will be used.
If you could try the above commands and then send a screenshot of the output (With the values blurred out of course) we could see if the CSV is looking up to par
Looking forward to your reply
Cheers
Oliwer Sundgren- Rambo363636Aug 22, 2024Brass ContributorHi Oliwer,
Thanks for looking into it. Could you pls advise where should I type "$Data or $data.Value and $data.Name to see that everything outputs correctly". In the code or in the excel ?. Can you pls give me a sample. Sorry i am new to powershell.
Regards,
Ram- oliwer_sundgrenAug 22, 2024Iron Contributor
Hi Rambo, no problem at all we've all been there 🙂
In Powershell after you've ran these 2 lines in your code
$keyVaultName = "marketing-test"
$data = Import-Csv "C:\Users\Ram.Muppaneni\Downloads\marketing.csv"Then just simply type $Data.Value in the Powershell terminal and see what the output is
What editor are you using to write the Powershell script?
See my example below from Visual Studio Code where I have one line of code, I run that line and then in the terminal I type $Data to get the value of that variableWhen you do this, $Data.Value should give you some output. If possible, share a screenshot of that output. With sensitive information blurred or replaced with test data.
Let me know if you dont follow and I can guide you further 🙂
Cheers