Skip to content

Instantly share code, notes, and snippets.

@bohack
Created December 6, 2017 15:25
Show Gist options
  • Save bohack/d52f009471ebc6476f43bfd01b757b09 to your computer and use it in GitHub Desktop.
Save bohack/d52f009471ebc6476f43bfd01b757b09 to your computer and use it in GitHub Desktop.
Secure Local Password Check
<#
.Synopsis
Verify Local SAM store
.DESCRIPTION
This function takes a user name and a password as input and will verify if the combination is correct. The function returns a boolean based on the result. The script defaults to local user accounts, but a remote computername can be specified in the -ComputerName parameter.
.NOTES
Name: Test-LocalCredential
Author: Jaap Brasser
Version: 1.0
DateUpdated: 2013-05-20
.PARAMETER UserName
The samaccountname of the Local Machine user account
.PARAMETER Password
The password of the Local Machine user account
.PARAMETER ComputerName
The computer on which the local credentials will be verified
.EXAMPLE
Test-LocalCredential -username jaapbrasser -password Secret01
Description:
Verifies if the username and password provided are correct on the local machine, returning either true or false based on the result
#>
function Test-LocalCredential {
[CmdletBinding()]
Param
(
[string]$UserName,
[string]$ComputerName = $env:COMPUTERNAME,
[string]$Password
)
if (!($UserName) -or !($Password)) {
Write-Warning 'Test-LocalCredential: Please specify both user name and password'
} else {
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext('machine',$ComputerName)
$DS.ValidateCredentials($UserName, $Password)
}
}
$usrname = 0
$secpasswd = 0
$usrname = Read-Host -Prompt "Enter Username: "
$secpasswd = Read-Host -Prompt "Enter Password: " -AsSecureString
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secpasswd)
$value = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)
Test-LocalCredential -username $usrname -password $value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment