Azure Key Vault Set Secret from PowerShell- Pattern Match Exception

Pavan Kumar 1 Reputation point
2020-08-25T15:37:55.327+00:00

I am trying to bulk insert secrets to my Key Vault using power shell and Its throwing me Secret Validation exception saying Pattern Mismatch- ^[0-9a-zA-Z-]+$

But same secret, if i am doing manually in the portal, its working fine.

Need some insights and help.

Azure Key Vault
Azure Key Vault
An Azure service that is used to manage and protect cryptographic keys and other secrets used by cloud apps and services.
1,126 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,381 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 45,091 Reputation points
    2020-08-25T18:32:26.81+00:00

    Please post the code you're using.

    1. Azure Powershell is outdated (i.e. it still works, but it's no longer supported)
    2. Use the newer Az module (azpsmigrate
    3. The secret is supposed to be a secure string. Is that what you're using? If not, see the help for the cmdlet and follow the examples given there.

  2. Pavan Kumar 1 Reputation point
    2020-08-25T18:49:09.603+00:00

    Here is the code along with error i am using. Thanks for the response
    $inputfile = Import-Csv C:\BCP\inputcsv.csv

    foreach($inp in $inputfile)

    {

    $Name = $inp.Name

    $secretvalue = $inp.secretvalue

    $Contenttype = $inp.Contenttype

    $Tags = @{$inp.TagName = $inp.TagValue}

    $Secret = ConvertTo-SecureString -String $secretvalue -AsPlainText -Force

    set-azkeyvaultsecret LTLkeyVault -Name $Name -SecretValue $secret -ContentType $Contenttype -Tag $Tags

    }
    set-azkeyvaultsecret : 'secretName' does not match expected pattern '^[0-9a-zA-Z-]+$'.
    At line:18 char:3

    • set-azkeyvaultsecret LTLkeyVault -Name $Name -SecretValue $secret ...
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : CloseError: (:) [Set-AzKeyVaultSecret], ValidationException
    • FullyQualifiedErrorId : Microsoft.Azure.Commands.KeyVault.SetAzureKeyVaultSecret

    set-azkeyvaultsecret : 'secretName' does not match expected pattern '^[0-9a-zA-Z-]+$'.
    At line:18 char:3

    • set-azkeyvaultsecret LTLkeyVault -Name $Name -SecretValue $secret ...
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : CloseError: (:) [Set-AzKeyVaultSecret], ValidationException
    • FullyQualifiedErrorId : Microsoft.Azure.Commands.KeyVault.SetAzureKeyVaultSecret