Advertisement
Guest User

CredTest

a guest
Feb 7th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. function CredTest($cred, $rekurze){
  2. $ErrorActionPreference = "Ignore"
  3. try{#passed cred CAN be $null
  4. $username = $cred.username
  5. $password = $cred.GetNetworkCredential().password
  6. }
  7. catch{
  8. }
  9. $CurrentDomain = "LDAP://" + ([ADSI]"").distinguishedName
  10. $domain = New-Object System.DirectoryServices.DirectoryEntry($CurrentDomain,$UserName,$Password)
  11. if ($domain.name -eq $null){
  12. write-host "Invalid creds." -ForegroundColor Red
  13. if($rekurze -ne 1){
  14. Write-Host "Try again"
  15. $cred = Get-Credential -UserName "domain\" -Message "You need a domain account:";
  16. CredTest $cred 1 #trying again. Once.
  17. }
  18. else{
  19. Write-Host "Script will be terminated: " -ForegroundColor Red
  20. [Environment]::Exit(0)
  21. }
  22. }
  23. else{
  24. write-host "Valid creds. " $cred.UserName -ForegroundColor Green
  25. return $cred
  26. }
  27. }
  28. $cred = CredTest $cred
  29. return $cred
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement