Powershell 部分匹配字符串密码复杂性

Powershell Partial Match Strings Password Complexity

我有两个来自 csv 文件的字符串:

$Password = "dkjhfd22dnrhfg"
$Username = "NewUser22d"

我正在尝试检查: 不包含用户的帐户名称或用户全名中超过两个连续字符的部分。

如果匹配到3个或更多字符我想在这里添加:

if(($password -cmatch '[a-z]') -and ($password -cmatch '[A-Z]') -and ($password -match '\d') -and (($password).length -ge 18))

问题:

  1. 如何匹配?

  2. 如何添加到现有的 if 语句。

也许运行一个函数会更容易

因为您真的不需要寻找(更多)最小密码/用户名匹配

Function Check-Password{
    param(
        [string]$Username,
        [string]$Password,
        [int]$MatchLength
    )
    for($i = 0; $i -ne ($Username.Length - ($MatchLength - 1)); $i++){
        $Username.Substring($i, $MatchLength) | Where-Object {$Password -like "*$_*"}
    }
}

Check-Password -Username "NewUser22d" -Password "dkjhfd22dnrhfg" -MatchLength 3

这将return

22d