Ac9293
Ac9293

Reputation: 85

How to validate azure resource naming conventions using PowerShell

I want to check the name (for example azure vnet) provided in script to validate for azure vnet naming convention (Like it should not have any special character and length 2-64)

I am using below code but it is not working if string $name has special character in it.

It is working only for 0-9 and a-z.

$name = "zzz"
$name -cmatch "^[0-9a-z]*$"

Need a code to check a string which has for special character. If it has special character it should return true.

Upvotes: 0

Views: 782

Answers (2)

Tony Ju
Tony Ju

Reputation: 15609

Try this one, if there is a special character in name, it will return true.

$name = "_"
$name -notmatch "[0-9a-zA-Z]"

Upvotes: 1

rAJ
rAJ

Reputation: 1433

Try :

$name = "hello@"
if($name -match '[^a-zA-Z0-9]')
{
    Write-Host "special character found"
}
else
{
    Write-Host "special character not found"
}

Upvotes: 1

Related Questions