gpt4 book ai didi

powershell - 在 Windows Powershell 中验证密码匹配

转载 作者:行者123 更新时间:2023-12-04 19:00:18 26 4
gpt4 key购买 nike

我正在创建一个脚本来处理我工作的学区的无人值守域加入。我们有几个处理 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"

最佳答案

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

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/

关于powershell - 在 Windows Powershell 中验证密码匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38901752/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com