测试密码在powershell脚本中具有特殊字符

时间:2013-06-22 00:56:42

标签: powershell passwords special-characters

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

 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,则会通过并且脚本结束。我想要它再次提示,只有当我输入cats1234 $时才会通过。

谢谢!

1 个答案:

答案 0 :(得分: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