Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. function New-Credential{
  2. $counter = 0
  3. $more = $true
  4. while($more){
  5. if($counter -ge 3){
  6. Write-Warning -Message ('Take a deep breath and perhaps a break. You have entered your password {0} times incorrectly' -f $counter)
  7. Write-Warning -Message ('Please wait until {0} to try again to avoid risking locking yourself out.' -f $((Get-Date).AddMinutes(+15).ToShortTimeString()))
  8. Start-Sleep -Seconds 30
  9. }
  10.  
  11. # Collect the username and password and store in credential object.
  12. $userName = Read-Host -Prompt 'Please enter your domain\username'
  13. $password = Read-Host -AsSecureString -Prompt 'Please enter your password'
  14.  
  15. try{
  16. $credential = New-Object System.Management.Automation.PSCredential $userName,$password
  17.  
  18. # Build the current domain
  19. $currentDomain = 'LDAP://{0}' -f $credential.GetNetworkCredential().Domain
  20.  
  21. # Get the user\password. The GetNetworkCredential only works for the passwrod because the current user
  22. # is the one who entered it. Shouldn't be accessible to anything\one else.
  23. $userName = $credential.GetNetworkCredential().UserName
  24. $password = $credential.GetNetworkCredential().Password
  25.  
  26. }
  27. catch{
  28. Write-Warning -Message ('There was a problem with what you entered: {0}' -f $_.exception.message)
  29. continue
  30. }
  31.  
  32. # Do a quick query against the domain to authenticate the user.
  33. $dom = New-Object System.DirectoryServices.DirectoryEntry($currentDomain,$userName,$password)
  34. # If we get a result back with a name property then we're good to go and we can store the credential.
  35. if($dom.name){
  36. Write-Output $credential
  37. $more = $false
  38. Remove-Variable password -Force
  39. }
  40. else{
  41. $counter++
  42.  
  43. Write-Warning -Message ('The password you entered for {0} was incorrect. Attempts {1}. Please try again.' -f $userName,$counter)
  44. }
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement