验证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
and this is where I got that from: http://techibee.com/powershell/compare-secure-strings-entered-through-powershell/422
这篇关于验证Windows Powershell中的密码是否匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!