Home > Software engineering >  Powershell System.Security.Cryptography.X509Certificates.X509Certificate2 issues
Powershell System.Security.Cryptography.X509Certificates.X509Certificate2 issues

Time:01-07

i can get the code to work but im having issues when i try to narrow down my choices. I'm using powershell 7.3.

This code works fine for what i need here:

Add-Type -AssemblyName System.Security

$ValidCerts = [System.Security.Cryptography.X509Certificates.X509Certificate2[]](Get-ChildItem 'Cert:\CurrentUser\My')
$Cert = [System.Security.Cryptography.X509Certificates.X509Certificate2UI]::SelectFromCollection($ValidCerts, 'Choose a certificate', 'Choose a certificate', 0)

$Pin = Read-Host "Enter your PIN: " -AsSecureString
$Script:cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $Cert,$Pin

===========================================================================

This code here doesnt work at all:

Add-Type -AssemblyName System.Security

$ValidCerts = [System.Security.Cryptography.X509Certificates.X509Certificate2[]](Get-ChildItem 'Cert:\CurrentUser\My') | ?{$_.EnhancedKeyUsageList.FriendlyName -like 'Client Authentication'}
$Cert = [System.Security.Cryptography.X509Certificates.X509Certificate2UI]::SelectFromCollection($ValidCerts, 'Choose a certificate', 'Choose a certificate', 0)

$Pin = Read-Host "Enter your PIN: " -AsSecureString
$Script:cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $Cert,$Pin

error that I'm getting:

MethodException: Cannot find an overload for "SelectFromCollection" and the argument count: "4".

If i could get an answer to this that would be nice but i can use the top code just fine

CodePudding user response:

Just move the parentheses to the end of your filter:

$ValidCerts = [System.Security.Cryptography.X509Certificates.X509Certificate2[]](Get-ChildItem  Cert:\CurrentUser\My | ?{$_.EnhancedKeyUsageList.FriendlyName -like 'Client Authentication'})

CodePudding user response:

Per comments, the SelectFromCollection method takes a X509Certificate2Collection as the first parameter. In in your second example you’re trying to pass an array of X509Certificate2 but there’s no overload that accepts it as the first parameter.

What you need to do is construct a new X509Certificate2Collection from your filtered results and pass that to SelectFromCollection instead - see the documentation for an example.

If you modify your second example like this it will work:

# get a filtered list of certificates to select from
$ValidCerts = [System.Security.Cryptography.X509Certificates.X509Certificate2[]] (
    Get-ChildItem "Cert:\CurrentUser\My"
) | where-object {
    $_.EnhancedKeyUsageList.FriendlyName -like "Client Authentication"
};

# convert the array of X509Certificate2s into a X509Certificate2Collection
$collection = new-object System.Security.Cryptography.X509Certificates.X509Certificate2Collection(
    @(, $ValidCerts)
);

# pass the X509Certificate2Collection to SelectFromCollection instead
$Cert = [System.Security.Cryptography.X509Certificates.X509Certificate2UI]::SelectFromCollection(
    $collection, # <-- X509Certificate2Collection 
    "Choose a certificate",
    "Choose a certificate",
    0
);
  • Related