2016-08-11 118 views
1

我正在创建一个脚本来处理我工作的学区的无人参与域加入。我们有几个处理sysprep的IT人员,所以我创建了一个脚本来加密密码以用于Add-Computer。在Windows中验证密码匹配Powershell

我遇到的麻烦是脚本需要两个密码条目,如果它们不匹配就重新启动,但如果它们继续,则继续。我已经尝试到目前为止:

$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" 
+0

一般来说加密的密码是错误的解决方案,当攻击者获得管理员权限时,他将得到包含加密密码和加密密钥的数据库。 – zaph

+0

我认为,但我们是一个相对较小的地区,而且我的同事们每次都厌倦了为域加入而输入证书,所以我的脚本在第一次登录时运行,然后(通过unattend.xml)另一次C:\ Windows \ Panther中的批处理文件删除所有内容,一旦域加入。此外,加密的密码是作为主图像创建的,在域加入之前永远不会获得互联网访问权限。只要域加入,文件就会再见。有人不得不偷盗硬盘。 –

回答

2

您在比较两个安全字符串,因此您需要先解密它们。这里有你想要做什么的实现:

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/

+1

我永远无法让自己喜欢回到纯文本字符串的方法。只是为了有一个替代方案:'(New-Object PSCredential'。',$ SecureString).GetNetworkCredential()。Password'。唉,它仍然适度复杂。 –

+0

谢谢你的工作!我的下一个问题是,如果密码不同,我该如何重新启动它?所以这个脚本让你继续重试直到它们匹配。 –

+1

回答了我自己的问题!我会更新代码。 –