Skip to content

Instantly share code, notes, and snippets.

@huzzeytech
Last active October 18, 2018 14:55
Show Gist options
  • Save huzzeytech/3fdd920e5e8d8dc20f2a104b519e3cbc to your computer and use it in GitHub Desktop.
Save huzzeytech/3fdd920e5e8d8dc20f2a104b519e3cbc to your computer and use it in GitHub Desktop.
<#$userName = $args[0]
$password = $args[1]
$hostName = $args[2]
#>
$useName = admin
$password = "testing123"
$hostName = windows10
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$principalContext = New-Object System.DirectoryServices.AccountManagement.PrincipalContext("Machine","$hostname")
Function Validate-Credentials{
$validate=$principalContext.ValidateCredentials($userName, $passWord)
if($validate)
{
return $true
}
elseif($validate -eq $false)
{
throw "Bad username or Password"
}
else{
throw $_.Exception.InnerException
}
}
try
{
Validate-Credentials
}
catch
{
if($Error[0].Exception.InnerException -match "Access is denied.")
{
$iP=Test-Connection $hostname | select IPV4Address
$iP=$IP[0].IPV4Address.IPAddressToString
$hostname= $iP
Validate-Credentials
}
else
{
throw $_.Exception.InnerException
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment