PowerShell / SecretManagement

PowerShell module to consistent usage of secrets through different extension vaults
MIT License
317 stars 46 forks source link

Exception on type initializer #120

Closed bravo-kernel closed 3 years ago

bravo-kernel commented 3 years ago

Awesome module and 0.9.1 works great on one of our machines but running Get-SecretVault is throwing the following exception on a pristine Windows Server 2016:

Get-SecretVault : The type initializer for 'Microsoft.PowerShell.SecretManagement.RegisteredVaultCache' threw an exception.

Any ideas/pointers on what could be causing this?

Thanks in advance ❤️

PaulHigin commented 3 years ago

This exception is thrown usually when an incompatible .Net binary is loaded. SecretManagement and SecretStore are both PowerShell binary modules and so only one version of the module binary can be loaded in .Net before you need to start a new instance of PowerShell. This is just a known issue with PowerShell binary modules.

To be safe you should always uninstall any earlier versions of SecretManagement (and SecretStore if you are using the extension vault) before installing a new version. Also try starting a new PowerShell instance (but this isn't always needed).

Get-Module -Name Microsoft.PowerShell.Secret* -List
Uninstall-Module Microsoft.PowerShell.SecretStore -Force
Uninstall-Module Microsoft.PowerShell.SecretManagement -Force
...

Also you can usually get more information on the error by looking at error exception and inner exceptions. Many times PowerShell will bury the original exception.

$err = Get-Error
$err.Exception
$err.Exception.InnerException
...
bravo-kernel commented 3 years ago

Thank you for the quick reply and the very good Get-Error pointer.

The issue was indeed caused by an incompatible .Net version and resolved by:

  1. installing .Net 4.7.2
  2. then re-installing the modules as explained above

Much love ❤️