0

我正在尝试构建一个脚本来检查密码是否包含字母和数字以及特殊字符。这是我到目前为止所拥有的:

 Write-Output " Enter db Password - DO NOT Use spaces - " 
 Write-Output " No part of the username can be contained in the password"

  do {$pdb=read-host " Must have 8 characters include at least 1 number & 1 special character i.e. help@menow1"

   $string = $pdb
   $pat = "^[a-zA-Z0-9\s]+$"
   $special = [regex]"[^a-zA-Z0-9]"
   }

  while (!$pdb -or 

      !($pdb.length -ge 8) -or

      ($pdb.Contains($pat)) -or 

      ($pdb.Contains($special))

      ) 

它完成了我想要的大部分工作。如果我输入密码 cat 并按回车,它会再次提示输入密码。如果我放入cats1234,则通过并且脚本结束。我希望它再次提示并且只有在我输入 cat1234$ 时才会通过。

谢谢!

4

1 回答 1

1

试试这个:

do {
    $psw=read-host "Must have 8 characters include at least 1 number & 1 special character i.e. help@menow1"
} while (
    $psw.length -lt 8 -or $psw -notmatch '[^a-zA-Z0-9]' -or $psw -notmatch '\d'
) 

$psw
于 2013-06-22T10:14:38.820 回答