powershell icon indicating copy to clipboard operation
powershell copied to clipboard

[BUG] Connect-PnPOnline - Keyset does not exist

Open Nibushi opened this issue 5 years ago • 18 comments

Reporting an Issue or Missing Feature

If I run Connect-PnPOnline locally using a certificate (stored in the local machine certificate store) and supplying the parameters (Tenant, Thumbprint and ClientId ) the command fails when using PnP.PowerShell (0.3.33):

Connect-PnPOnline -Url $Url -Tenant $tenant -Thumbprint $thumbprint -ClientId $clientId

Error message: Connect-PnPOnline: Keyset does not exist Message : Keyset does not exist Stacktrace : at PnP.PowerShell.Commands.Base.ConnectOnline.ProcessRecord() in D:\a\powershell\powershell\src\Commands\Base\ConnectOnline.cs:line 293 at System.Management.Automation.CommandProcessor.ProcessRecord() ScriptLineNumber : 1

If I run the exact same command and parameters (Tenant, Thumbprint and ClientId ) on my local machine but use SharePointPnPPowerShellOnline (3.22.2006.2) the Connect-PnPOnline command works. The same command and parameters also works inside an Azure Function without any issues.

Expected behavior

Would expect the command to successfully establish a connection with SharePoint Online

Actual behavior

Error message: Connect-PnPOnline: Keyset does not exist Message : Keyset does not exist Stacktrace : at PnP.PowerShell.Commands.Base.ConnectOnline.ProcessRecord() in D:\a\powershell\powershell\src\Commands\Base\ConnectOnline.cs:line 293 at System.Management.Automation.CommandProcessor.ProcessRecord() ScriptLineNumber : 1

Steps to reproduce behavior

Create App registration using certificate Supply tenant, thumbprint and clientId as variables

Connect-PnPOnline -Url $Url -Tenant $tenant -Thumbprint $thumbprint -ClientId $clientId

What is the version of the Cmdlet module you are running?

0.3.33 - Have tried previous versions as well.

As I say above, the exact same command and parameter values works using the SharePointPnPPowerShellOnline module.

Which operating system/environment are you running PnP PowerShell on?

  • [x ] Windows
  • [ ] Linux
  • [ ] MacOS
  • [ ] Azure Cloud Shell
  • [ ] Azure Functions
  • [ ] Other : please specify

Nibushi avatar Jan 12 '21 08:01 Nibushi

Hi Nibushi

I have just tried upgraded to the nightly version 0.3.33 and tried to connect using exact same command (albeit with a certificate I created a while back) and I was able to connect fine then get all the lists in the SharePoint site (see image below). I am using Windows

image

From memory KeySet is to do with the certificate are you using the same certificate when you auth usingSharePointPnPPowerShellOnline

Also check the certificate created has a primary key and has been generated correctly.

LeonArmston avatar Jan 12 '21 09:01 LeonArmston

Hi @LeonArmston, thanks for testing this. The certificate was created using New-PnPAzureCertificate and is the exact same certificate which I use with SharePointPnPPowerShellOnline. I've retried the command again and I am still getting the KeySet issue.

If I run the following version of Connect-PnPOnline using the same certificate I can connect successfully: Connect-PnPOnline -Url $url -CertificatePath "C:\democerts\PnPPowerShellApp_2021.pfx" -Tenant $tenant -CertificatePassword $securePass -ClientId $clientId

Nibushi avatar Jan 12 '21 10:01 Nibushi

@Nibushi : We identified an issue that might cause this. We're working on/testing a fix as we speak. It can mean you will have to recreate your certificate though.

erwinvanhunen avatar Jan 13 '21 12:01 erwinvanhunen

@erwinvanhunen no problem. Thanks for letting me know. Happy to test once the fix has been released.

Nibushi avatar Jan 13 '21 12:01 Nibushi

We just release 0.3.34-nightly. A few things have changed:

New-PnPAzureCertificate now supports a -Store parameter, allowing you to immediately add a certificate to the store. We also changed the way we generate the certificate when running on Windows. Register-PnPAzureAAApp will by default now use popup windows when executed on Microsoft Windows to walk people through the consent flow. Register-PnPAzureADApp will by default export the certificates to the file system too.

erwinvanhunen avatar Jan 13 '21 13:01 erwinvanhunen

I ran the following command using build 0.3.35 and can confirm that the step through process worked which was really good: Register-PnPAzureADApp -DeviceLogin -ApplicationName TestApp -Tenant $tenant -CertificatePassword (ConvertTo-SecureString -String $certPassword -AsPlainText -Force) -Store CurrentUser

I was then able to connect using Connect-PnPOnline -Url $Url -Tenant $tenant -Thumbprint $thumbprint -ClientId $clientId

You may need to update the Example text in the help as I had to build the command up based on the text in the other examples or if you have instructions on how to update the documentation I would be happy to help out and do it.

I noticed that the certificate was put in the local user certificate, this may have been the issue initially as I had the certificate installed in the local machine personal certs. I'll go back and look at that myself, however, the issue is now resolved.

Really like the walk through process, works really well.

Nibushi avatar Jan 14 '21 15:01 Nibushi

I'm trying to understand this thread and I'm not sure what the resolution is. The key was to use the -store command at the time of certificate generation?

I am getting the keyset does not exist error. However when I go to the certificate management, remove the cert, then add the cert back, everything works. However after a week, I will get keyset does not exist again.

My import is: Import-PfxCertificate -Exportable -CertStoreLocation Cert:\LocalMachine\My -FilePath .$certname.pfx #Install certificate

Thank you in advance.

elementlu avatar Jan 10 '22 20:01 elementlu

Hi @elementlu , I never experienced the issue again when using the new releases of the module. I think the combination of things being fixed behind the scenes and me ensuring that the certificate was in my local user's certificate store resolved the issues I had.

I've recently used the New-PnPAzureCertificate and the Register-PnPAzureADApp commands for different demos and both worked successfully. I've never had to re-import the certificate periodically.

Sorry I can't be of more use.

Nibushi avatar Jan 23 '22 19:01 Nibushi

i imported the cert into the user store instead of system and the keyset error has not comeback. still odd though

elementlu avatar Jan 24 '22 20:01 elementlu

Hi @elementlu , I have been able to reproduce the "Connect-PnPOnline: Keyset does not exist" message again using the Register-PnPAzureADApp cmdlet.

I normally use VS Code for PowerShell so that is where I do most of my testing and using the certificate created by Register-PnPAzureADApp works in there without any issues. I ran the following command:

Register-PnPAzureADApp -ApplicationName $ApplicationName -OutPath "E:\tempPnPCerts" -CommonName "xxxxxxxxxxxxxxxx" -ValidYears 1 -CertificatePassword $pwd -Store CurrentUser -Tenant "xxxxxxxxxxxx" -GraphApplicationPermissions Group.ReadWrite.All,User.Read.All -SharePointApplicationPermissions Sites.FullControl.All, User.Read.All -Interactive

The app and the certificate were created successfully and Connect-PnPOnline worked without any issues in VSCode.

However, as a test I tried using the Windows Terminal and PowerShell 7.2.1 using the same certificate and I received the Keyset does not exist message. After re-importing the certificate which had been created I was able to successfully connect using Connect-PnPOnline.

Not sure what console you are using but I have been able to reproduce this behavior three times this morning.

Update If I run the Register-PnPAzureADApp from the Windows Terminal (pwsh 7.2.1) I can successfully run Connect-PnPOnline from both the Windows Terminal and VSCode..........I luuuurvvee computers :-)

@erwinvanhunen, is this something that you can test? I've only tried this on a single machine at the moment so I can't rule out something particular with my Windows Terminal set up but it hasn't been altered in anyway.

Nibushi avatar Jan 25 '22 10:01 Nibushi

@Nibushi I was doing my testing with PowerShell ISE. The server I was trying to run this job didn't have VScode.

I won't be able to test Register-PnPAzureADApp as I don't have the appropriate rights. Will see if I can get someone else to test.

elementlu avatar Jan 26 '22 22:01 elementlu

Hello folks, I did not read the whole thread in detail but in my case I had a functional connection two days ago.....today I tried to debug my script again and got the same error message and I checked my script on and on.... at the end I tried to open my ISE PowerShell with adminstrator rights and that worked for me...... I have no clue what the difference is, I could imagine that only started as admin, ISE is able to read my local installed certificates or something like that!

It was again a simple root cause and a simple solution.... I did not have to change my code or my certificates or something else :D

JohnJonnyJonson avatar Apr 06 '22 08:04 JohnJonnyJonson

After getting this same problem, I can indeed confirm that certificates from the LocalMachine store can only be read from an elevated process when using connect-pnponline. I have not tested adding the SharePointPnPPowerShellOnline back to the mix, but I do remember removing it because it was no longer necessary (I thought) after upgrading to v 1.11 of PnPOnline. But preliminary test does show that it works when started elevated and gets the keyset error when not elevated.

That is, until the next day (shutdown of VM in between). Now the Connect-PnPOnline with the exact same set of parameters, having a self-signed cert in the LocalMachine store (albeit deleted yesterday and imported again today from PFX file), DOES work again. No idea why....

BartRamharter avatar Oct 24 '22 14:10 BartRamharter

For you Information: Run the PowerShell as Admin and it woks to Access the Computer Certificate Store

weyCC81 avatar Nov 10 '22 13:11 weyCC81

That is what 'run in elevated process' means... But as I mentioned, the day after, it also works when running as (non-admin) regular process. Still no idea why.

BartRamharter avatar Nov 10 '22 13:11 BartRamharter

Hi,

Sorry to revive an old thread, but I was wondering if there was any update on this issue. I'm trying to use a device cert to connect to sharepoint using PnP.Powershell 2.1.1 and getting the same 'Keyset does not exist' message.

I've tried creating a machine cert (on two machines) once manually and once with New-PnPAzureCertificate -Store LocalMachine. I can see them in my cert store (as a non-localadmin user) and I've uploaded the .cer files to my pre-existing app registration in azure. The thumbprints match in the app and in the cert stores.

Using the same connect-pnponline command from an admin pwsh session works fine.

As far as I know there's no security on the device cert that would be preventing a non-admin user from accessing them. As a normal user I can get-childitem the cert from the cert:\localmachine\my store.

Any advice would be gratefully received :)

iainfm avatar Jun 01 '23 08:06 iainfm

I think I've figured this out. It is a permissions thing. To fix:

Open mmc/local computer certs Locate the cert under Personal\Certificates Right-click -> All Tasks -> Manage private keys Grant the user(s)/group(s) read permission

iainfm avatar Jun 01 '23 08:06 iainfm

Maybe it is useful to mention, that if you put the certificate in your personal folder (this means, if you select -store CurrentUser, instead of LocalMachine), there is no option to manage private keys and the certificate private key can only be read when you run the script as admin. For productive environments and computers, I think, this is actually a good feature, and after thinking if I should move my dev-certificate to LocalMachine, I kept it in the personal store. For now, I have to run the console as administrator for App-Only requests.

achimismaili avatar Mar 25 '25 08:03 achimismaili