Guest User

Untitled

a guest
Feb 26th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. $cred = Get-Credential #Read credentials
  2. $username = $cred.username
  3. $password = $cred.GetNetworkCredential().password
  4.  
  5. # Get current domain using logged-on user's credentials
  6. $CurrentDomain = "LDAP://" + ([ADSI]"").distinguishedName
  7. $domain = New-Object System.DirectoryServices.DirectoryEntry($CurrentDomain,$UserName,$Password)
  8.  
  9. if ($domain.name -eq $null)
  10. {
  11. write-host "Authentication failed - please verify your username and password."
  12. exit #terminate the script.
  13. }
  14. else
  15. {
  16. write-host "Successfully authenticated with domain $domain.name"
  17. }
  18.  
  19. function Test-Credential {
  20. <#
  21. .SYNOPSIS
  22. Takes a PSCredential object and validates it against the domain (or local machine, or ADAM instance).
  23.  
  24. .PARAMETER cred
  25. A PScredential object with the username/password you wish to test. Typically this is generated using the Get-Credential cmdlet. Accepts pipeline input.
  26.  
  27. .PARAMETER context
  28. An optional parameter specifying what type of credential this is. Possible values are 'Domain','Machine',and 'ApplicationDirectory.' The default is 'Domain.'
  29.  
  30. .OUTPUTS
  31. A boolean, indicating whether the credentials were successfully validated.
  32.  
  33. #>
  34. param(
  35. [parameter(Mandatory=$true,ValueFromPipeline=$true)]
  36. [System.Management.Automation.PSCredential]$credential,
  37. [parameter()][validateset('Domain','Machine','ApplicationDirectory')]
  38. [string]$context = 'Domain'
  39. )
  40. begin {
  41. Add-Type -assemblyname system.DirectoryServices.accountmanagement
  42. $DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::$context)
  43. }
  44. process {
  45. $DS.ValidateCredentials($credential.UserName, $credential.GetNetworkCredential().password)
  46. }
  47. }
  48.  
  49. #Get credentials
  50. $credential_ok = 0
  51. while ($credential_ok -ne 1)
  52. {
  53. $credential = get-credential
  54. $result = connect-qadservice -service *domain_name* -credential $credential
  55. [string]$result_string = $result.domain
  56. if ($result_string -eq "*domain_name*")
  57. {
  58. $credential_ok = 1
  59. #authenticated
  60. }
  61. else
  62. {
  63. #failed
  64. }
  65. }
  66. $username = $credential.username
  67. $password = $credential.GetNetworkCredential().password
  68.  
  69. $date = get-date
  70. Add-Content "c:lbinInstall_log.txt" "Successfully authenticated XP script as $username $date"
  71.  
  72. param([string]$preloadServiceAccountUserName = "")
  73.  
  74. function HarvestCredentials()
  75. {
  76.  
  77. [System.Management.Automation.PSCredential]$credentialsOfCurrentUser = Get-Credential -Message "Please enter your username & password" -UserName $preloadServiceAccountUserName
  78.  
  79. if ( $credentialsOfCurrentUser )
  80. {
  81. $credentialsOfCurrentUser = $credentialsOfCurrentUser
  82. }
  83. else
  84. {
  85. throw [System.ArgumentOutOfRangeException] "Gui credentials not entered correctly"
  86. }
  87.  
  88. Try
  89. {
  90.  
  91.  
  92. # validate the credentials are legitimate
  93. $validateCredentialsTest = (new-object System.DirectoryServices.DirectoryEntry ("WinNT://"+$credentialsOfCurrentUser.GetNetworkCredential().Domain), $credentialsOfCurrentUser.GetNetworkCredential().UserName, $credentialsOfCurrentUser.GetNetworkCredential().Password).psbase.name
  94. if ( $null -eq $validateCredentialsTest)
  95. {
  96. throw [System.ArgumentOutOfRangeException] "Credentials are not valid. ('" + $credentialsOfCurrentUser.GetNetworkCredential().Domain + '' + $credentialsOfCurrentUser.GetNetworkCredential().UserName + "')"
  97. }
  98. else
  99. {
  100. $t = $host.ui.RawUI.ForegroundColor
  101. $host.ui.RawUI.ForegroundColor = "Magenta"
  102. Write-Output "GOOD CREDENTIALS"
  103. $host.ui.RawUI.ForegroundColor = $t
  104. }
  105. }
  106. Catch
  107. {
  108.  
  109. $ErrorMessage = $_.Exception.Message
  110. $FailedItem = $_.Exception.ItemName
  111. $StackTrace = $_.Exception.StackTrace
  112.  
  113. $t = $host.ui.RawUI.ForegroundColor
  114. $host.ui.RawUI.ForegroundColor = "Red"
  115.  
  116. Write-Output "Exception - $ErrorMessage"
  117. Write-Output "Exception - $FailedItem"
  118. Write-Output "Exception - $StackTrace"
  119.  
  120. $host.ui.RawUI.ForegroundColor = $t
  121.  
  122. throw [System.ArgumentOutOfRangeException] "Attempt to create System.DirectoryServices.DirectoryEntry failed. Most likely reason is that credentials are not valid."
  123. }
  124.  
  125. }
  126.  
  127.  
  128. Try
  129. {
  130.  
  131. HarvestCredentials
  132.  
  133. }
  134. Catch
  135. {
  136. $ErrorMessage = $_.Exception.Message
  137. $FailedItem = $_.Exception.ItemName
  138. $StackTrace = $_.Exception.StackTrace
  139.  
  140. $t = $host.ui.RawUI.ForegroundColor
  141. $host.ui.RawUI.ForegroundColor = "Red"
  142.  
  143. Write-Output "Exception - " + $ErrorMessage
  144. Write-Output "Exception - " + $FailedItem
  145. Write-Output "Exception - " + $StackTrace
  146.  
  147. $host.ui.RawUI.ForegroundColor = $t
  148.  
  149. Break
  150. }
  151. Finally
  152. {
  153. $Time=Get-Date
  154. Write-Output "Done - " + $Time
  155. }
  156.  
  157. .TestCredentials.ps1 -preloadServiceAccountUserName "mydomainmyusername"
Add Comment
Please, Sign In to add comment