Verify Passwords Match in Windows Powershell

前端 未结 1 504
深忆病人
深忆病人 2021-01-05 11:53

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 wil

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-05 12:47

    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"
    }
    

    and this is where I got that from: http://techibee.com/powershell/compare-secure-strings-entered-through-powershell/422

    also possibly relevant: https://www.roelvanlisdonk.nl/2010/03/23/show-password-in-plaintext-by-using-get-credential-in-powershell/

    0 讨论(0)
提交回复
热议问题