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

How To Copy Folder in Legacy Windows with Long File Names?

$sourceDirectory='D:\SMBSHARE\LONGPATH' $destinationDirectory='\\FILESERVER\LONGPATH' function copyFolderWithLongNames($sourceDirectory,$destinationDirectory){ $sourceParent=split-path $sourceDirectory -parent $sourceChild=split-path $sourceDirectory -leaf $destinationParent=split-path $destinationDirectory -parent $destinationChild=split-path $destinationDirectory…

PowerShell: How To Set IP and Domain Restrictions to Specific IIS Sites

# Enable IP Filtering Feature in IIS using PowerShell Install-WindowsFeature Web-IP-Security Restart-Service W3SVC # Optional:…

Quick Script to Mount Remote UNC Paths as Local Drive Letters

Simple Mount: # Mapping a drive $username='domain\testAdmin' $password='PASSWORD' $remotePath='\\UNCSERVER\SHAREPATH\Backup' $unavailableDriveLetters=(Get-Volume).DriveLetter|sort $availableDriveLetters=.{(65..90|%{[char]$_})|?{$_ -notin $unavailableDriveLetters}} [char]$firstAvailableDriveLetter=$availableDriveLetters[0] $command="net…