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.
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 47,941 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

  3. Gary Vella 0 Reputation points
    2025-10-04T13:42:22.9+00:00

    The name of the secret is the problem. You cannot have dashes or other special characters in the name except for $. It is not referring to the secret Value.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.