Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $cred = Get-Credential #Read credentials
- $username = $cred.username
- $password = $cred.GetNetworkCredential().password
- # Get current domain using logged-on user's credentials
- $CurrentDomain = "LDAP://" + ([ADSI]"").distinguishedName
- $domain = New-Object System.DirectoryServices.DirectoryEntry($CurrentDomain,$UserName,$Password)
- if ($domain.name -eq $null)
- {
- write-host "Authentication failed - please verify your username and password."
- exit #terminate the script.
- }
- else
- {
- write-host "Successfully authenticated with domain $domain.name"
- }
- function Test-Credential {
- <#
- .SYNOPSIS
- Takes a PSCredential object and validates it against the domain (or local machine, or ADAM instance).
- .PARAMETER cred
- A PScredential object with the username/password you wish to test. Typically this is generated using the Get-Credential cmdlet. Accepts pipeline input.
- .PARAMETER context
- An optional parameter specifying what type of credential this is. Possible values are 'Domain','Machine',and 'ApplicationDirectory.' The default is 'Domain.'
- .OUTPUTS
- A boolean, indicating whether the credentials were successfully validated.
- #>
- param(
- [parameter(Mandatory=$true,ValueFromPipeline=$true)]
- [System.Management.Automation.PSCredential]$credential,
- [parameter()][validateset('Domain','Machine','ApplicationDirectory')]
- [string]$context = 'Domain'
- )
- begin {
- Add-Type -assemblyname system.DirectoryServices.accountmanagement
- $DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::$context)
- }
- process {
- $DS.ValidateCredentials($credential.UserName, $credential.GetNetworkCredential().password)
- }
- }
- #Get credentials
- $credential_ok = 0
- while ($credential_ok -ne 1)
- {
- $credential = get-credential
- $result = connect-qadservice -service *domain_name* -credential $credential
- [string]$result_string = $result.domain
- if ($result_string -eq "*domain_name*")
- {
- $credential_ok = 1
- #authenticated
- }
- else
- {
- #failed
- }
- }
- $username = $credential.username
- $password = $credential.GetNetworkCredential().password
- $date = get-date
- Add-Content "c:lbinInstall_log.txt" "Successfully authenticated XP script as $username $date"
- param([string]$preloadServiceAccountUserName = "")
- function HarvestCredentials()
- {
- [System.Management.Automation.PSCredential]$credentialsOfCurrentUser = Get-Credential -Message "Please enter your username & password" -UserName $preloadServiceAccountUserName
- if ( $credentialsOfCurrentUser )
- {
- $credentialsOfCurrentUser = $credentialsOfCurrentUser
- }
- else
- {
- throw [System.ArgumentOutOfRangeException] "Gui credentials not entered correctly"
- }
- Try
- {
- # validate the credentials are legitimate
- $validateCredentialsTest = (new-object System.DirectoryServices.DirectoryEntry ("WinNT://"+$credentialsOfCurrentUser.GetNetworkCredential().Domain), $credentialsOfCurrentUser.GetNetworkCredential().UserName, $credentialsOfCurrentUser.GetNetworkCredential().Password).psbase.name
- if ( $null -eq $validateCredentialsTest)
- {
- throw [System.ArgumentOutOfRangeException] "Credentials are not valid. ('" + $credentialsOfCurrentUser.GetNetworkCredential().Domain + '' + $credentialsOfCurrentUser.GetNetworkCredential().UserName + "')"
- }
- else
- {
- $t = $host.ui.RawUI.ForegroundColor
- $host.ui.RawUI.ForegroundColor = "Magenta"
- Write-Output "GOOD CREDENTIALS"
- $host.ui.RawUI.ForegroundColor = $t
- }
- }
- Catch
- {
- $ErrorMessage = $_.Exception.Message
- $FailedItem = $_.Exception.ItemName
- $StackTrace = $_.Exception.StackTrace
- $t = $host.ui.RawUI.ForegroundColor
- $host.ui.RawUI.ForegroundColor = "Red"
- Write-Output "Exception - $ErrorMessage"
- Write-Output "Exception - $FailedItem"
- Write-Output "Exception - $StackTrace"
- $host.ui.RawUI.ForegroundColor = $t
- throw [System.ArgumentOutOfRangeException] "Attempt to create System.DirectoryServices.DirectoryEntry failed. Most likely reason is that credentials are not valid."
- }
- }
- Try
- {
- HarvestCredentials
- }
- Catch
- {
- $ErrorMessage = $_.Exception.Message
- $FailedItem = $_.Exception.ItemName
- $StackTrace = $_.Exception.StackTrace
- $t = $host.ui.RawUI.ForegroundColor
- $host.ui.RawUI.ForegroundColor = "Red"
- Write-Output "Exception - " + $ErrorMessage
- Write-Output "Exception - " + $FailedItem
- Write-Output "Exception - " + $StackTrace
- $host.ui.RawUI.ForegroundColor = $t
- Break
- }
- Finally
- {
- $Time=Get-Date
- Write-Output "Done - " + $Time
- }
- .TestCredentials.ps1 -preloadServiceAccountUserName "mydomainmyusername"
Add Comment
Please, Sign In to add comment