BetterCredentials icon indicating copy to clipboard operation
BetterCredentials copied to clipboard

Test-Credential throws exception

Open jaspain opened this issue 7 years ago • 1 comments

Test-Credential and Find-Credential don’t work as expected. The problem lies with the Find function in CredentialManagement.cs. Because CredWrite alters the names of credentials, you are using the function FixTarget to make this alteration transparent when credentials are retrieved. Thus FixTarget needs to be applied to the filter argument of the Find function to make that transparent as well.

Also in the Find function CredEnumerate returns false when it fails. In addition to some exception-worthy problems, the failure may be due to there being no credentials that pass the filter (ERROR_NOT_FOUND). In the case of ERROR_NOT_FOUND, the Find function should return a null result rather than throwing an exception.

In my pull request, I am proposing changes to the Find function, and based on these changes I also added some additional examples for Test-Credential and Find-Credential.

jaspain avatar May 19 '18 16:05 jaspain

Not exactly sure if it's the same issue, but with version 4.5, every command I've tried throws an exception. I'm using PSV 5.1.14409.1012 on Windows 7:

[16:45:18][00:00:01.0211021] C:\> Find-Credential | ? username -like *test* | select username
[16:45:23][00:00:00.0050005] C:\> get-credential test -store
Exception calling "Load" with "1" argument(s): "Element not found"
At C:\Program Files\WindowsPowerShell\Modules\BetterCredentials\4.5\BetterCredentials.psm1:351 char:21
+ ...             $Credential = [CredentialManagement.Store]::Load($UserNam ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : Win32Exception


UserName                     Password
--------                     --------
test     System.Security.SecureString


[16:45:40][00:00:01.8001800] C:\> Find-Credential | ? username -like *test* | select username

UserName
--------
test


[16:45:59][00:00:00.0050005] C:\> Find-Credential test
Exception calling "Find" with "1" argument(s): "Element not found"
At C:\Program Files\WindowsPowerShell\Modules\BetterCredentials\4.5\BetterCredentials.psm1:84 char:5
+     [CredentialManagement.Store]::Find($Filter)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : Win32Exception

[16:46:15][00:00:00.0250025] C:\> Test-Credential test
Exception calling "Find" with "1" argument(s): "Element not found"
At C:\Program Files\WindowsPowerShell\Modules\BetterCredentials\4.5\BetterCredentials.psm1:122 char:16
+ ...        return [CredentialManagement.Store]::Find($target).Count -gt 0
+                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : Win32Exception

[16:46:32][00:00:00.0100010] C:\> Remove-Credential test
Exception calling "Delete" with "3" argument(s): "Element not found"
At C:\Program Files\WindowsPowerShell\Modules\BetterCredentials\4.5\BetterCredentials.psm1:217 char:9
+         [CredentialManagement.Store]::Delete($Target, $Type, $false)
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : Win32Exception

Verayth avatar Jul 12 '18 20:07 Verayth