Posted On September 4, 2020

PowerShell: Test Domain Username & Password Credential

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Test Domain Username & Password Credential

Version 2:

function testCredential($username,$password){
    # Get current domain using logged-on user's credentials
    $isDomainJoined=$env:USERDOMAIN -ne $env:COMPUTERNAME 
    if($isDomainJoined){
        $domain="LDAP://"+([ADSI]"").distinguishedName # Legacy method without importing ActiveDirectory module
        $login=New-Object System.DirectoryServices.DirectoryEntry($domain,$username,$password)
        try{
            if($null -ne $login.name) {
                write-host "$username credential is valid" -foregroundcolor green
                return $true
            }else{
                write-warning "invalid credentials"
                return $false
                }
        }catch{
            write-warning $_
            return $false
            }
    }else{
        if(!(get-command psexec.exe)){
            [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
            if (!(Get-Command choco.exe -ErrorAction SilentlyContinue)) {
                Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
                }
            $null=choco install sysinternals -y
        }
        $pinfo = New-Object System.Diagnostics.ProcessStartInfo
        $pinfo.FileName = "psexec.exe"
        $pinfo.RedirectStandardError = $true
        $pinfo.RedirectStandardOutput = $true
        $pinfo.UseShellExecute = $false
        $pinfo.Arguments = "\\$env:computername -u $username -p $password -s cmd /c hostname"
        $p = New-Object System.Diagnostics.Process
        $p.StartInfo = $pinfo
        $p.Start() | Out-Null
        $p.WaitForExit()
        if($p.ExitCode -eq 0){
            return $true
        }else{
            return $false
        }
    }
}

Version 1:

# Obtain credential from user, this can be set directly when calling function
$cred = Get-Credential 
$username = $cred.username
$password = $cred.GetNetworkCredential().password

function testCredential($username,$password){
	# Get current domain using logged-on user's credentials
	$domain = "LDAP://" + ([ADSI]"").distinguishedName
	$login = New-Object System.DirectoryServices.DirectoryEntry($domain,$username,$password)
	try{
		if($login.name -ne $null) {
			write-host "$username credential is valid" -foregroundcolor green
			return $true
		}else{
			write-warning "invalid credentials"
			return $false
			}
	}catch{
		write-warning $_
		return $false
		}
}
testCredential $username $password

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

JavaScript: Build a Tribute Page

Demo: https:// codepen.io/dragoncoin/pen/EZJxYY HTML Code: <html><head> <body> <img src="http://www.animationxpress.com/wp-content/uploads/2015/10/Mahatma-Gandhi.jpg"> <h1>Mahatma Gandhi</h1> <div class="hidden"> <h2>"You must…

PowerShell: Delete Files Older Than 30 Days

Have you ever run into C:\ volumes reaching critical thresholds because certain applications or users…

How To Upgrade NextCloud 22.1.1 to 22.2.0 When Deployed with Kubernetes & Helm

Step 1: Navigate to nextcloud > html > edit version.php <?php $OC_Version = array(22,1,1,2); $OC_VersionString…