jeudi 31 octobre 2019

How to fix the -like which searches for charakters

first of all i'am very new to Powershell Programming and iam really sorry if this is a trivial question.

I'am trying to write a Password generator in Powershell everything works fine expect the if Statement. Iam askign if the Password containes any special charakters or numbers. If it doesnt contain any of them it should output an error if it contains numbers or charakter it should wirte the Password.

Iam trying to do this with an Array and ASCII. Iam writing the charakters into an array with a for loop and later on iam converting them to the specific character i want.

This is for a School Project iam working on. I've tried to remove the brackets in the IF statement but then it shows the password even without the special charakters and numbers.

$RandomePassword = "abC!231"
for($ASCII = 33  ; $ASCII -le 64 ; $ASCII++)
{
    $char += @($ASCII)
} 

if ($RandomePassword -like ([char[]]$char -join "|"))
{
    write (($RandomePassword -split '' | Sort-Object {Get-Random}) -join '')
}
else{
    write "ERROR"
}

The Expected result would be:

$RandomePassword = "Abc!12" -> writes Password on Comand line

$RandomePassword = "Abc" -> wirtes error

Thank you for your time and help

Aucun commentaire:

Enregistrer un commentaire