Skip to content

Instantly share code, notes, and snippets.

@Nillth
Forked from evetsleep/New-Credential.ps1
Created December 6, 2021 22:39
Show Gist options
  • Save Nillth/6ba7fc8e0e723cf4620b2d947fe3f8d2 to your computer and use it in GitHub Desktop.
Save Nillth/6ba7fc8e0e723cf4620b2d947fe3f8d2 to your computer and use it in GitHub Desktop.
New-Credential
function New-Credential{
$counter = 0
$more = $true
while($more){
if($counter -ge 3){
Write-Warning -Message ('Take a deep breath and perhaps a break. You have entered your password {0} times incorrectly' -f $counter)
Write-Warning -Message ('Please wait until {0} to try again to avoid risking locking yourself out.' -f $((Get-Date).AddMinutes(+15).ToShortTimeString()))
Start-Sleep -Seconds 30
}
# Collect the username and password and store in credential object.
$userName = Read-Host -Prompt 'Please enter your domain\username'
$password = Read-Host -AsSecureString -Prompt 'Please enter your password'
try{
$credential = New-Object System.Management.Automation.PSCredential $userName,$password
# Build the current domain
$currentDomain = 'LDAP://{0}' -f $credential.GetNetworkCredential().Domain
# Get the user\password. The GetNetworkCredential only works for the passwrod because the current user
# is the one who entered it. Shouldn't be accessible to anything\one else.
$userName = $credential.GetNetworkCredential().UserName
$password = $credential.GetNetworkCredential().Password
}
catch{
Write-Warning -Message ('There was a problem with what you entered: {0}' -f $_.exception.message)
continue
}
# Do a quick query against the domain to authenticate the user.
$dom = New-Object System.DirectoryServices.DirectoryEntry($currentDomain,$userName,$password)
# If we get a result back with a name property then we're good to go and we can store the credential.
if($dom.name){
Write-Output $credential
$more = $false
Remove-Variable password -Force
}
else{
$counter++
Write-Warning -Message ('The password you entered for {0} was incorrect. Attempts {1}. Please try again.' -f $userName,$counter)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment