title: Powershell Verify local Credentials
body: <p>So in my script I want to not only have the user enter and store credentials in a variable but be able to verify that the password matches the admin password on the target system. So far the only way I have found to do this is by putting the actual password unecrypted in the script and comparing it to the one the user enters. That is a huge security flaw and to remedy it I was wondering if I could get the admin password using a gwmi query (SID?) as an object and compare that to the secure string the user enters.</p>
  
  <p>Here is my flawed code I am using right now.</p>
  
  <pre><code>Do
  {
      $password = $null
      $password = read-host "Enter the Administrator Password" -assecurestring
      $AdminPass = ConvertTo-SecureString "adminpassword" -AsPlainText -Force
      $pwd1_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
      $pwd2_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($AdminPass))
      if ($pwd1_text -cne $pwd2_text) {Write-Host -ForegroundColor Red "Incorrect Password"; $password = $null}
      $count ++
      $tries = 3 - $count
      if ($password -eq $null) {Write-Host -ForegroundColor Yellow "$tries Attempts Remaining"}
      if ($count -eq 3) {Write-Host -ForegroundColor Red "$count Unsuccessful Password Attempts. Exiting..."; exit} 
  }While ($password -eq $null)
  $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "$ComputerName\Administrator",$password
  </code></pre>