Test-Credential.ps1
<#PSScriptInfo
.DESCRIPTION Simulates an Authentication Request in a Domain envrionment using a PSCredential Object. Returns $true if both Username and Password pair are valid. .VERSION 1.1 .GUID 6a18515f-73d3-4fb4-884f-412395aa5054 .AUTHOR Thomas Malkewitz @dotps1 .TAGS PSCredential .RELEASENOTES Corrected Parameter Type for -Credential to properly support pipeline input. #> [OutputType([Bool])] Param ( [Parameter( ValueFromPipeLine = $true, ValueFromPipelineByPropertyName = $true )] [Alias( 'PSCredential' )] [ValidateNotNull()] [System.Management.Automation.PSCredential] [System.Management.Automation.Credential()] $Credential = [System.Management.Automation.PSCredential]::Empty, [Parameter()] [String] $Domain = $env:USERDOMAIN ) Begin { [void][System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices.AccountManagement") $principalContext = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::Domain, $Domain) } Process { $networkCredetial = $Credential.GetNetworkCredential() return $principalContext.ValidateCredentials($networkCredetial.UserName, $networkCredetial.Password) } End { $principalContext.Dispose() } |