在Powershell Password Generator中排除特定单词

时间:2019-06-18 10:09:31

标签: powershell

我创建了一个脚本来创建随机密码,其格式与Microsoft重置密码时在O365中使用的格式相同。如果随机生成的密码包含任何可能潜在冒犯的指定值,那么我希望脚本忽略它们并重新运行循环。

我尝试使用-contains和-like语句,但是输出仍然包含其中一些值。

write-host "Enter number of passwords you require generating" - 
ForegroundColor Green
$Run = read-host
foreach($i in 1..$Run)
 {
    If($i=$Run){                           
    $result = ""
    $set1 = "bcdfghjklmnpqrstvwxyz".ToCharArray()
    $set2 = "aeiou".ToCharArray()
    $result += $set1 | Get-Random
    $result += $set2 | Get-Random
    $result += $set1 | Get-Random

$DisabledPass=$result.Substring(0,1).ToUpper(1)+$result.Substring(1,2)+(Get-Random -Minimum 10000 -Maximum 99999)

$valuesToLookFor = @(
'*Sex*',
'*Bum*',
'*Cow*',
'*Fuc*',
'*Wog*',
'*Cum*',
'*God*',
'*Gob*',
'*Fuk*')

if ($DisabledPass -contains $valuesToLookFor) {continue}
$disabledpass |Out-File c:\admin\passwords.csv -Append
Write-Host "$DisabledPass" -ForegroundColor Green
}
 }

脚本成功运行,但是输出中包含的密码包含我要忽略的值。

1 个答案:

答案 0 :(得分:1)

类似这样的东西:

$set1 = "bcdfghjklmnpqrstvwxyz".ToCharArray()
$set2 = "aeiou".ToCharArray()
$badWords = "(Sex|Bum|Cow|Fuc|Wog|Cum|God|Gob|Fuk)"

$count = Read-Host -Prompt "Enter number of passwords you require generating"

1..$count | ForEach-Object {
    do
    {
        $result = ""
        $result += $set1 | Get-Random
        $result += $set2 | Get-Random
        $result += $set1 | Get-Random

        $password = (Get-Culture).TextInfo.ToTitleCase($result) + (Get-Random -Minimum 10000 -Maximum 99999)
    } while ($password -match $badWords)

    Write-Host "Generated password: $password" -ForegroundColor Green
}