在 Windows Powershell 中验证密码匹配 [英] Verify Passwords Match in Windows Powershell
问题描述
我正在为我工作的学区创建一个脚本来处理无人参与的域加入.我们有几个处理 sysprep 的 IT 人员,所以我正在创建一个脚本来加密密码以用于 Add-Computer.
I'm creating a script to handle unattended domain joining for the school district I work at. We have several IT guys who handle sysprep, so I'm creating a script that will encrypt passwords to use for Add-Computer.
我遇到的问题是有一个脚本需要两个密码条目,如果它们不匹配则重新启动,但如果它们匹配则继续.到目前为止我尝试过的:
What I am having trouble with is having a script that takes two password entries, and restarts if they don't match, but continues if they do. What I've tried so far:
$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
}
我想让脚本自动让用户重试,直到两个密码匹配.
I would like to have the script automatically make the user retry until both passwords match.
想通了!这是供参考的代码.感谢 RowdyVinson!
Figured it out! Here's the code for reference. Thanks to 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"
推荐答案
您要比较两个安全字符串,因此您需要先解密它们.这是您尝试执行的操作:
You're looking to compare two secure strings, so you'll need to decrypt them first. Here's an implementation of what you're trying to do:
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
这篇关于在 Windows Powershell 中验证密码匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!