验证Windows Powershell中的密码匹配

时间:2016-08-11 16:29:49

标签: powershell passwords verification

我正在创建一个脚本来处理我工作的学区的无人参与域加入。我们有几个处理sysprep的IT人员,因此我创建了一个脚本,用于加密用于Add-Computer的密码。

我遇到的问题是有一个脚本需要两个密码输入,如果它们不匹配则重新启动,但如果它们匹配则继续。到目前为止我尝试过的事情:

$s = {write-host "running script}
&$s
$pwd1 = Read-Host -AsSecureString "Enter Password"
$pwd2 = Read-Host -AsSecureString "Enter Again"
If($pwd1 -ceq $pwd2) {
Write-host "match"
} else {
&$s
}

我想让脚本自动重试用户,直到两个密码都匹配。

编辑:想出来!这是代码供参考。感谢RowdyVinson!

do {
Write-Host "I am here to compare the password you are entering..."
$pwd1 = Read-Host "Password" -AsSecureString
$pwd2 = Read-Host "Re-enter Password" -AsSecureString
$pwd1_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd1))
$pwd2_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd2))
}
while ($pwd1_text -ne $pwd2_text)
Write-Host "Passwords matched"

1 个答案:

答案 0 :(得分:4)

您希望比较两个安全字符串,因此您需要先解密它们。以下是您要执行的操作的实现:

Write-Host "Hey..!! I am here to compare the password you are entering..."
$pwd1 = Read-Host "Passowrd" -AsSecureString
$pwd2 = Read-Host "Re-enter Passowrd" -AsSecureString
$pwd1_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd1))
$pwd2_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd2))


if ($pwd1_text -ceq $pwd2_text) {
Write-Host "Passwords matched"
} else {
Write-Host "Passwords differ"
}

这就是我从http://techibee.com/powershell/compare-secure-strings-entered-through-powershell/422

得到的地方

也可能相关:https://www.roelvanlisdonk.nl/2010/03/23/show-password-in-plaintext-by-using-get-credential-in-powershell/