locked
Why can't PowerShell find my certificate? RRS feed

  • Question

  • I have been running the following code for two days now and it worked just fine:

    if ((Test-Path cert:\CurrentUser\MY\$myCert.Thumbprint) -eq $true){
     $certificate = (Get-Item cert:\CurrentUser\MY\$myCert.Thumbprint)
    } else {
     $certificate = (Get-Item cert:\LocalMachine\MY\$myCert.Thumbprint)
    }

    I have confirmed that the cert with the thumbprint is still installed in the CurrentUser\Personal\Certificates area, and I've confirmed that when I load the certificate (the $myCert varialbe) that the thumbprint is in there, but just today this line of code stopped finding the certificate thumbprint.

    I've rebooted and cleared out just about everything I can think of. Not that it matters because everybody has their issues, but this is somewhat urgent. I'm hoping this is just something stupid since it was working earlier and no PS above it was modified in any way.

    Any ideas?


    STom

    Monday, September 24, 2012 11:17 PM

Answers

  • Yan,

    The variable $myCert is always loaded, especially just a few lines of code right about the code you see in the thread. I ended up adding another string variable:

    [string]$myCertTP = $myCert.Thumbprint

    And I changed my code to:

    if ((Test-Path cert:\CurrentUser\MY\$myCertTP) -eq $true){
     $certificate = (Get-Item cert:\CurrentUser\MY\$myCertTP)
    } else {
     $certificate = (Get-Item cert:\LocalMachine\MY\$myCertTP)
    }

    This worked.

    I will take a look at the links however.


    STom

    • Marked as answer by Yan Li_ Monday, October 1, 2012 1:42 AM
    Wednesday, September 26, 2012 6:34 PM

All replies

  • Hi,

    I would like to know whether the variable $myCer is always loaded, and have you run your powershell as Admin? As the script worked before and you haven't do anything else to change it, and the certificates did stay there.

    Please also refer to the below links for using powershell to find certificates:

    Search-CertificateStore

    http://poshcode.org/2207

    List Certs With Powershell

    http://community.spiceworks.com/topic/208697-list-certs-with-powershell

    Regards,

    Yan Li


    Yan Li

    TechNet Community Support

    Wednesday, September 26, 2012 8:30 AM
  • Yan,

    The variable $myCert is always loaded, especially just a few lines of code right about the code you see in the thread. I ended up adding another string variable:

    [string]$myCertTP = $myCert.Thumbprint

    And I changed my code to:

    if ((Test-Path cert:\CurrentUser\MY\$myCertTP) -eq $true){
     $certificate = (Get-Item cert:\CurrentUser\MY\$myCertTP)
    } else {
     $certificate = (Get-Item cert:\LocalMachine\MY\$myCertTP)
    }

    This worked.

    I will take a look at the links however.


    STom

    • Marked as answer by Yan Li_ Monday, October 1, 2012 1:42 AM
    Wednesday, September 26, 2012 6:34 PM
  • hey,

    Check the case of the certificate thumbprint it need to be in uppercase else convert it as upper case.

    if the case is lower then get-item returns nothing ... hope this helps

    prabhakar

    Monday, January 19, 2015 6:46 PM
  • This helped me. Thank you Prabhakar!

    Thursday, December 29, 2016 9:04 PM