While 函数 - 循环匹配新 AD 用户创建脚本的输入密码失败

While 函数 - 循环匹配新 AD 用户创建脚本的输入密码失败

我正在创建一个带有基本变量的脚本,以便通过 PS7 在 AD 中创建用户,而不是远程进入 AD 并手动创建。它会运行几个问题,然后我的脚本将在最后运行以创建 AD 用户。

当我不知道 while 循环时,这是非常原始的起点

 $NewPassword = (Read-Host -Prompt 'Please enter User New Password' -AsSecureString)
 $NewPasswordMatch = (Read-Host -Prompt 'Please re-enter User New Password' -AsSecureString)

 $newp1 = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($NewPassword))
 $newp2 = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($NewPasswordMatch))

 if ($newp1 -CEQ $newp2) {
 Write-Host "Passwords matched" -ForegroundColor Green
 } else {
 Write-Host "Passwords incorrect - please reset in AD once user is 
 created" -ForegroundColor Red
 }

我已经做好了基本准备,但我对密码循环功能感到困惑。我曾经让它循环,但它不会写入主机“密码不匹配,请重试”。我已经玩了这么多次,现在我还没能把它恢复到那个状态,所以我请求帮助,希望我犯了一个愚蠢的错误,以至于我看不懂代码。

  • 我要求技术人员输入两次用户密码,以确保没有输入错误
  • 我有脚本来检查 PW1 是否匹配 PW2。
  • 我可以让它检查,并产生“OK”或“Not OK”的屏幕提示
  • 我无法让它提示“密码不匹配”,然后循环回到顶部让技术人员再次输入密码,直到正确为止。当密码正确时,它将转到下一个脚本

我对 PowerShell 还很陌生,除了在线网络研讨会和书籍之外,我都是自学的,我理解 While 的前提,但不是 100% 理解其中的陷阱。此脚本是在创建 AD 帐户之前,完整脚本是在你遇到最后一个问题时创建它。之后它将运行 new-aduser 等。

while( $true){ 

$NewPassword = (Read-Host -Prompt 'Please enter User New Password' -AsSecureString)
$NewPasswordMatch = (Read-Host -Prompt 'Please re-enter User New Password' -AsSecureString)

$newp1 = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($NewPassword))
$newp2 = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($NewPasswordMatch))
try{
     if ($newp1 -CEQ $newp2){
     Write-Host "Password Accepted" -ForegroundColor Green
        }
    Break
    }
catch{
    Write-Host "Passwords do not match, please try again" -ForegroundColor Red
    }
}

答案1

将逻辑分成两部分功能并使用脚本范围根据密码来决定是否循环匹配包括其大小写敏感性。您可以省略使用该try{}catch{}块来实现该逻辑,因为它实际上并不需要。因此,用这个替换该部分的逻辑,您就应该一切就绪了。

电源外壳

Function SetPass(){
    $NewPassword = (Read-Host -Prompt 'Please enter User New Password' -AsSecureString)
    $NewPasswordMatch = (Read-Host -Prompt 'Please re-enter User New Password' -AsSecureString)
    
    $script:newp1 = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($NewPassword))
    $script:newp2 = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($NewPasswordMatch))
    PassMatch
}

Function PassMatch(){
    if($newp1 -cmatch $newp2){Write-Host "Password Accepted" -ForegroundColor Green}
    if($newp1 -cnotmatch $newp2){Write-Host "Passwords do not match, please try again" -ForegroundColor Red;setpass}
    }
SetPass;

Write-Host "Run rest of logic" -ForegroundColor Yellow;

答案2

老实说,我会让脚本为你生成一个密码并显示出来。它还能确保密码足够安全且不可猜测。

这是一个创建好密码的简单脚本:

function CreatePassword
{
        $cap = "BCDFGHJKLMNPQRSTVWXYZ".ToCharArray()
        $let = "bcdfghjklmnpqrstvwxz".ToCharArray()
        $vow = "aeiou".ToCharArray()
        $pw = (get-random -InputObject $cap)
        $pw = $pw + (get-random -InputObject $vow)
        $pw = $pw + (get-random -InputObject $let)
        $pw = $pw + (get-random -InputObject $vow)
        $pw = $pw + (get-random -Minimum 1111 -Maximum 9999)

        return $pw
}

$password = CreatePassword

write-host "The password is: $password"

但如果您真的想进入循环询问密码的情况,则以下脚本可以起作用:

function AskForAPassword
{
    $passwordsmatch = $false
    while( $passwordsmatch -eq $false)
    {
        $password1 = read-host "Please enter a new password"
        $password2 = read-host "Please enter the password again"
        if( $password1 -eq $password2)
        {
            write-host "The passwords match." -ForegroundColor green
            $passwordsmatch = $true
        }
        else
        {
            write-host "The passwords did not match. Please try again." -ForegroundColor red
        }
    }

    return $password1 | ConvertTo-SecureString -AsPlainText -Force
}

$password = AskForAPassword

write-host "The password is: $password"

注意,当我将密码转换为安全字符串时,脚本将打印:密码是:system.security.securestring

相关内容