0
votes

I am trying to run the following PS script to back-up all of the Secrets, in a specific Azure Key Vault:

$secret = Get-AzureKeyVaultSecret –VaultName 'testkeyvault-1' |
ForEach-Object {
$Name = $_."Name"
Backup-AzureKeyVaultSecret -Secret $Name -OutputFile 'C:\Backup.blob'
}

Though this is failing with the following PS error, any help would be appreciated:

Backup-AzureKeyVaultSecret : Cannot bind parameter 'Secret'. Cannot convert  the "SQLSecret" value of type "System.String" to type    "Microsoft.Azure.Commands.KeyVault.Models.Secret".
At line:4 char:36
+ Backup-AzureKeyVaultSecret -Secret $Name -OutputFile 'C:\Backup.blob'
+                                    ~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Backup- AzureKeyVaultSecret], ParameterBindingException
+ FullyQualifiedErrorId :  CannotConvertArgumentNoMessage,Microsoft.Azure.Commands.KeyVault.BackupAzureKeyVaultSecret
2
Try using -Secret $_ instead of assigning only the name value. (Not familiar with this function, so not sure if this will work; but the error implies this is likely the issue).JohnLBevan
Hi thanks for the reply, it sees the argument as null if I add $_ Backup-AzureKeyVaultSecret : Cannot validate argument on parameter 'Secret'. The argument is null or empty. Provide an argument that is not null or empty, and then try the commandFuzzy Logic
Interesting; that implies that $_."Name" would also be returning null (since in PS the property of a null object is itself null)... so I'd have though you should have received the same error when using $Name previously... unless this isn't the first item in the list.JohnLBevan
Try changing ForEach-Object { to Where-Object {$_} | ForEach-Object {. i.e. that should filter out any null values in the list. Od that any would be returned; but maybe that will help. Beyond that; don't think I can come up with any other good suggestions I'm afraid...JohnLBevan
Hi regarding -Secret $_."Name" I had left the quotes off, included I now get the following output: Backup-AzureKeyVaultSecret : Cannot bind parameter 'Secret'. Cannot convert the "TestUsername" value of type "System.String" to type "Microsoft.Azure.Commands.KeyVault.Models.Secret". At line:4 char:36 + Backup-AzureKeyVaultSecret -Secret $_."Name" -OutputFile 'C:\Backup.b ... Fuzzy Logic

2 Answers

1
votes

Try this:

[string]$VaultName = 'testkeyvault-1'
Get-AzureKeyVaultSecret –VaultName $VaultName |
    ForEach-Object {
        Backup-AzureKeyVaultSecret `
            –VaultName $VaultName `
            -Name $_."Name" `
            -OutputFile ('C:\Backup_{0}.blob' -f $_."Name")
    }

Related documentation:

0
votes

You are missing the parameter "Vault Name" inside the LOOP