Ok, This is a weird behavior I see between Powershell ISE and Powershell Studio. The code below gives totally opposite results when evaluated.
The Powershell ISE throws the value as true as it should be.
Powershell Studio throws the value as False which is incorrect. Debugging shows its receiving the correct values to evaluate. Anyone encountered this issue or can you emulate the same. I am working with Powershell Studio 2017 and Windows powershell version 5.0. Any insight is much appreciated.
$ACCOUNT = 'UserName'
$domain = 'DomainName'
$AccountFQDN = $domain + '\' + $ACCOUNT
$Password = "******"
#In the event, user verifies verifies the password for account
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain
$pc = New-Object System.DirectoryServices.AccountManagement.PrincipalContext($ct, $domain)
$value = $pc.ValidateCredentials($ACCOUNT , $Password,[System.DirectoryServices.AccountManagement.ContextOptions]::Negotiate).ToString()
If ($Value -eq $true)
{
Write-Host ("Authentication Successful? - $value `n ")
}
ElseIf ($Value -eq $false)
{
Write-Host ("Authentication UnSuccessful? - $value `n ")
}
ValidateCredentials throws incorrect result
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
- lokesh.gunjugnur
- Posts: 2
- Last visit: Fri Jun 12, 2020 8:06 am
Re: ValidateCredentials throws incorrect result
The following is a bad construct and may easily cause many issues depending on where it is used.
The correct method for testing a Boolean is as follows:
A Boolean does not need to be tested against boolean and may fail under many circumstances.
You are also converting the options to a string. This is not necessary and a string "false" will always be true.
$value = $pc.ValidateCredentials($ACCOUNT , $Password,[System.DirectoryServices.AccountManagement.ContextOptions]::Negotiate).ToString()
The correct sybtax and construct for this is as follows:
Code: Select all
If ($Value -eq $true)
{
Write-Host ("Authentication Successful? - $value `n ")
}
ElseIf ($Value -eq $false)
{
Write-Host ("Authentication UnSuccessful? - $value `n ")
}
Code: Select all
If ($Value){
Write-Host ("Authentication Successful? - $value `n ")
}else{
Write-Host ("Authentication UnSuccessful? - $value `n ")
}
You are also converting the options to a string. This is not necessary and a string "false" will always be true.
$value = $pc.ValidateCredentials($ACCOUNT , $Password,[System.DirectoryServices.AccountManagement.ContextOptions]::Negotiate).ToString()
The correct sybtax and construct for this is as follows:
Code: Select all
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain
$pc = New-Object System.DirectoryServices.AccountManagement.PrincipalContext($ct, $domain)
if($pc.ValidateCredentials($ACCOUNT , $Password,'Negotiate')){
Write-Host 'Authentication Successful!'
}else{
Write-Host 'Authentication UnSuccessful!'
}
- lokesh.gunjugnur
- Posts: 2
- Last visit: Fri Jun 12, 2020 8:06 am
Re: ValidateCredentials throws incorrect result
Thanks for the clarification. Yes, its much cleaner and precise to evaluate the Boolean in the manner you explained. Its all good at my end now. I appreciate your time and the quick response.