Last active
October 18, 2018 14:55
-
-
Save huzzeytech/3fdd920e5e8d8dc20f2a104b519e3cbc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#$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