title: AD powershell validation
body: <p>I am writing a script such that it does the following:</p>
  <ul>
  <li>IP address configured (for historical reasons, we have static IP
  addresses)</li>
  <li>Change hostname</li>
  <li>Choose OU</li>
  <li>Join domain</li>
  <li>Add user to remote desktop user</li>
  </ul>
  <p>I have managed to code 4 of the above except I cannot get validating credentials to join the domain.  The problem I have is that it doesn't handle errors properly.  So it accepts empty password and jpoins the domain.</p>
  <pre><code>    $correct = $false
  do{
  $dompassword = Read-Host &quot;Enter the domain administrator password for the host to join the mydomain domain&quot; -AsSecureString
  $credentials = New-Object System.Management.Automation.PsCredential(&quot;mydomain\administrator&quot;, $dompassword)
  
  
  try{
  Write-Host &quot;Adding $computername to the domain&quot;
  Add-Computer -DomainName &quot;mydomain&quot; -Credential $credentials -OUPath $ou
  Rename-Computer -NewName $computername -DomainCredential $credentials -Force
  $correct = $true
  }
  catch{
  Write-Host &quot;Password incorrect, please re-enter&quot;
  $correct = $false
  }
  </code></pre>
  <p>How do I error handle the following:</p>
  <ul>
  <li>wrong password</li>
  <li>empty password</li>
  </ul>