Last active
January 12, 2018 15:47
-
-
Save VimalShekar/94e5cf1a64c56f0913595bfb910a7f93 to your computer and use it in GitHub Desktop.
This file contains 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
# -- Get the full script here : https://github.com/VimalShekar/PowerShell/blob/master/CheckIsUserValid.ps1 | |
# This is used by some of the functions below | |
$logonUserSignature = | |
@' | |
[DllImport( "advapi32.dll" )] | |
public static extern bool LogonUser( String lpszUserName, | |
String lpszDomain, | |
String lpszPassword, | |
int dwLogonType, | |
int dwLogonProvider, | |
ref IntPtr phToken ); | |
'@ | |
$closeHandleSignature = | |
@' | |
[DllImport( "kernel32.dll", CharSet = CharSet.Auto )] | |
public static extern bool CloseHandle( IntPtr handle ); | |
'@ | |
$revertToSelfSignature = | |
@' | |
[DllImport("advapi32.dll", SetLastError = true)] | |
public static extern bool RevertToSelf(); | |
'@ | |
$AdvApi32 = Add-Type -MemberDefinition $logonUserSignature -Name "AdvApi32" -Namespace "PsInvoke.NativeMethods" -PassThru | |
$Kernel32 = Add-Type -MemberDefinition $closeHandleSignature -Name "Kernel32" -Namespace "PsInvoke.NativeMethods" -PassThru | |
$AdvApi32_2 = Add-Type -MemberDefinition $revertToSelfSignature -Name "AdvApi32_2" -Namespace "PsInvoke.NativeMethods" -PassThru | |
[Reflection.Assembly]::LoadWithPartialName("System.Security") | |
function IsLocalUserNamePasswordValid() | |
{ | |
param( | |
[String]$UserName, | |
[String]$Password | |
) | |
$Logon32ProviderDefault = 0 | |
$Logon32LogonInteractive = 2 | |
$tokenHandle = [IntPtr]::Zero | |
$success = $false | |
$DomainName = $null | |
#Attempt a logon using this credential | |
$success = $AdvApi32::LogonUser($UserName, $DomainName, $Password, $Logon32LogonInteractive, $Logon32ProviderDefault, [Ref] $tokenHandle) | |
return $success | |
} | |
function IsDomainUserNamePasswordValid() | |
{ | |
param( | |
[String]$UserName, | |
[String]$Password, | |
[String]$DomainName | |
) | |
$Logon32ProviderDefault = 0 | |
$Logon32LogonInteractive = 2 | |
$tokenHandle = [IntPtr]::Zero | |
$success = $false | |
#Attempt a logon using this credential | |
$success = $AdvApi32::LogonUser($UserName, $DomainName, $Password, $Logon32LogonInteractive, $Logon32ProviderDefault, [Ref] $tokenHandle) | |
return $success | |
} | |
#Example Usage: | |
#IsLocalUserNamePasswordValid -UserName TestLocalUser1 -Password Test@Pass1 | |
#IsDomainUserNamePasswordValid -UserName TestDomUser1 -Password Test@Pass1 -DomainName testdom.local |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment