SecretManagement icon indicating copy to clipboard operation
SecretManagement copied to clipboard

TypeInitializationException

Open SamLue opened this issue 5 years ago • 20 comments

If I try this Module on a Windows 2016 Server, I'm always getting the following error.

Test-SecretVault

Test-SecretVault : Der Typeninitialisierer für "Microsoft.PowerShell.SecretManagement.RegisteredVaultCache" hat eine Ausnahme verursacht. In Zeile:1 Zeichen:1

  • Test-SecretVault
  •   + CategoryInfo          : NotSpecified: (:) [Test-SecretVault], TypeInitializationException
      + FullyQualifiedErrorId : System.TypeInitializationException,Microsoft.PowerShell.SecretManagement.TestSecretVault
     Command
    
    

Maybe it could be interesting for the further development. If you need any Information about this error, feel free to contact me.

SamLue avatar Jan 07 '21 16:01 SamLue

Issue Label Bot is not confident enough to auto-label this issue. See dashboard for more details.

issue-label-bot[bot] avatar Jan 07 '21 16:01 issue-label-bot[bot]

Thanks @SamLue when this error occurs do you have any SecretVaults registered, what is the output of Get-SecretVault?

SydneyhSmith avatar Jan 19 '21 17:01 SydneyhSmith

Also what version of the module, and PowerShell are you using?

SydneyhSmith avatar Jan 19 '21 22:01 SydneyhSmith

Hello @SydneyhSmith,

thanks for your answer. Already the implicit Import-Module produces this error.

Also when I try to execute Get-SecretVault:

PS C:\Windows\system32> Get-SecretVault
Get-SecretVault : Der Typeninitialisierer für "Microsoft.PowerShell.SecretManagement.RegisteredVaultCache" hat eine
Ausnahme verursacht.
In Zeile:1 Zeichen:1
+ Get-SecretVault
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-SecretVault], TypeInitializationException
    + FullyQualifiedErrorId : System.TypeInitializationException,Microsoft.PowerShell.SecretManagement.GetSecretVaultC
   ommand

PS C:\Windows\system32>

Version is 0.9.0


PS C:\Windows\system32> Get-Module -ListAvailable Microsoft.PowerShell.SecretManagement


    Verzeichnis: C:\Program Files\WindowsPowerShell\Modules


ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Binary     0.9.0      Microsoft.PowerShell.SecretManag... {Register-SecretVault, Unregister-SecretVault, Get-SecretV...

Powershell-Version

PS C:\Windows\system32> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.14393.3866
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14393.3866
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

SamLue avatar Jan 20 '21 08:01 SamLue

I have this problem on every System with an Windows 10 1607 (Windows 10 Enterprise - IOT) Version Buildnumber


10.0.14393 14393

Maybe an important hint.

SamLue avatar Mar 11 '21 12:03 SamLue

Running this command (with the secretstore module installed v0.9.2) Register-SecretVault -Name SecretStore -ModuleName Microsoft.PowerShell.SecretStore -DefaultVault gives us the same error. This is on server 2016. Any more info i can provide to assist?

craftzneko avatar Mar 18 '21 11:03 craftzneko

Having the exact same issue on Server 2016:

PS Y:\> Register-SecretVault -Name SecretStore -ModuleName Microsoft.PowerShell.SecretStore -DefaultVault
Register-SecretVault : The type initializer for 'Microsoft.PowerShell.SecretManagement.RegisteredVaultCache'
threw an exception.
At line:1 char:1
+ Register-SecretVault -Name SecretStore -ModuleName Microsoft.PowerShe ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Register-SecretVault], TypeInitializationException
    + FullyQualifiedErrorId : System.TypeInitializationException,Microsoft.PowerShell.SecretManagement.Regis
   terSecretVaultCommand

And module and $PSversionTable info:

PS Y:\> get-module *secret*

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Binary     1.0.0      Microsoft.PowerShell.SecretManag... {Get-Secret, Get-SecretInfo, Get-SecretVault, Re...
Binary     1.0.2      microsoft.powershell.secretstore    {Get-SecretStoreConfiguration, Reset-SecretStore...


PS Y:\> $psVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.14393.4467
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14393.4467
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Matches Version and build number mentioned by @SamLue above

meestark avatar Jul 13 '21 21:07 meestark

@SydneyhSmith maybe you can change the status "Needs-Repro-Info" Or which further info do you need?

If you want reproduce this issue, setup a System with BuildVersion 10.0.14393

If you need more information feel free to contact me.

SamLue avatar Jul 26 '21 10:07 SamLue

Any news about it? I'm having the same issue on Windows Server 2016

Register-SecretVault -Name SecretStore -ModuleName Microsoft.PowerShell.SecretStore -DefaultVault Register-SecretVault : The type initializer for 'Microsoft.PowerShell.SecretManagement.RegisteredVaultCache' threw an exception. At line:1 char:1

  • Register-SecretVault -Name SecretStore -ModuleName Microsoft.PowerShe ...
  •   + CategoryInfo          : NotSpecified: (:) [Register-SecretVault], TypeInitializationException
      + FullyQualifiedErrorId : System.TypeInitializationException,Microsoft.PowerShell.SecretManagement.Regis
     terSecretVaultCommand
    

pierodamafalda avatar Sep 07 '21 15:09 pierodamafalda

Same error here (Windows Server 2016 10.0.14393 Build 14393):

PS C:\> Set-SecretStoreConfiguration -Default
Set-SecretStoreConfiguration : The type initializer for 'Microsoft.PowerShell.SecretStore.Utils' threw an exception.
At line:1 char:1
+ Set-SecretStoreConfiguration -Default
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Set-SecretStoreConfiguration], TypeInitializationException
    + FullyQualifiedErrorId : System.TypeInitializationException,Microsoft.PowerShell.SecretStore.SetSecretStoreConfiguration

PS C:\> Register-SecretVault -Name 'Test' -ModuleName 'SecretManagement.Keepass' -VaultParameters @{
>>     Path = "C:\Users\******\documents\Test.kdbx"
>>     UseMasterPassword = $true
>> }
Register-SecretVault : The type initializer for 'Microsoft.PowerShell.SecretManagement.RegisteredVaultCache' threw an exception.
At line:1 char:1
+ Register-SecretVault -Name 'Test' -ModuleName 'SecretManagement.Keepa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Register-SecretVault], TypeInitializationException
    + FullyQualifiedErrorId : System.TypeInitializationException,Microsoft.PowerShell.SecretManagement.RegisterSecretVaultCommand
PS C:\> Get-Module -ListAvailable Microsoft.PowerShell.SecretManagement


    Directory: C:\Program Files\WindowsPowerShell\Modules


ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Binary     1.1.1      Microsoft.PowerShell.SecretManag... {Register-SecretVault, Unregister-SecretVault, Get-SecretVault, Set-SecretVaultDefault...}
PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.14393.4583
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14393.4583
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

iRon7 avatar Nov 25 '21 12:11 iRon7

For what it is worth:

PS C:\> $Error[0].Exception.InnerException

Could not load file or assembly 'System.Runtime.InteropServices.RuntimeInformation, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

iRon7 avatar Nov 25 '21 16:11 iRon7

I've had the exact problem and it was resolved by installing .NET Framework 4.8 Full.

This will quickly show you the Full version installed (if any)

(Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse | Where-Object -FilterScript { $_.name -match 'Full' } | Get-ItemProperty | Where-Object {$_.PSChildName -eq 'Full'}).Version

kieranwalsh avatar Jan 24 '22 22:01 kieranwalsh

I am having the same issue as well on 2016 servers at .net 4.6.1. 2016 Servers on .net 4.8 work fine. @PaulHigin @SydneyhSmith is this similar to this issue on the secret store - https://github.com/PowerShell/SecretStore/issues/28?

montereyharris avatar Jan 25 '22 15:01 montereyharris

Repro information

Server 2016 OS Build Number PS C:\Users\x> $PSVersionTable

Name Value


PSVersion 5.1.14393.4583 PSEdition Desktop PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} BuildVersion 10.0.14393.4583 CLRVersion 4.0.30319.42000 WSManStackVersion 3.0 PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1

PS C:\Users\x> Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full'

CBS : 1 Install : 1 InstallPath : C:\Windows\Microsoft.NET\Framework64\v4.0.30319
Release : 394802 Servicing : 0 TargetVersion : 4.0.0 Version : 4.6.01586 PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4 PSChildName : Full PSDrive : HKLM PSProvider : Microsoft.PowerShell.Core\Registry

montereyharris avatar Jan 25 '22 16:01 montereyharris

Thanks for reporting this, and sorry for the delayed response, but I have been busy with other tasks.

Both SecretManagement and SecretStore have a dependency on 'System.Runtime.InteropServices', which is only supported in .NetFramework starting with version 4.7.

I will try to have a fix for this by end of week. In the meantime the workaround is to install .NetFramework 4.7 or greater.

PaulHigin avatar Jan 25 '22 19:01 PaulHigin

Ok, I have released Microsoft.PowerShell.SecretManagement v1.1.2 and Microsoft.PowerShell.SecretStore v1.0.6 that has the fix. I verified that they run correctly on WindowsPowerShell in WS2016 with .NetFramework 4.6.

https://www.powershellgallery.com/packages/Microsoft.PowerShell.SecretManagement/1.1.2 https://www.powershellgallery.com/packages/Microsoft.PowerShell.SecretStore/1.0.6

PaulHigin avatar Jan 27 '22 18:01 PaulHigin

I am still running into an error when attempting to register a secret vault. I am using the current releases provided by @PaulHigin listed above, including verifying I'm on .NetFramework greater than 4.7

PS C:\> Get-Date

Thursday, February 10, 2022 10:00:57 AM

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.2.1
PSEdition                      Core
GitCommitId                    7.2.1
OS                             Microsoft Windows 10.0.22000
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

PS C:\> (Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse | Where-Object -FilterScript { $_.name -match 'Full' } | Get-ItemProperty | Where-Object {$_.PSChildName -eq 'Full'}).Version
4.8.04161
PS C:\>
PS C:\>
PS C:\> Get-Module | Format-Table -AutoSize

ModuleType Version PreRelease Name                                  ExportedCommands
---------- ------- ---------- ----                                  ----------------
Manifest   7.0.0.0            Microsoft.PowerShell.Management       {Add-Content, Clear-Content, Clear-Item, Clear-ItemProperty…}
Binary     1.1.2              Microsoft.PowerShell.SecretManagement {Get-Secret, Get-SecretInfo, Get-SecretVault, Register-SecretVault…}
Binary     1.0.6              Microsoft.PowerShell.SecretStore      {Get-SecretStoreConfiguration, Reset-SecretStore, Set-SecretStoreConfiguration, Set-SecretStorePassword…}
Manifest   7.0.0.0            Microsoft.PowerShell.Security         {ConvertFrom-SecureString, ConvertTo-SecureString, Get-Acl, Get-AuthenticodeSignature…}
Manifest   7.0.0.0            Microsoft.PowerShell.Utility          {Add-Member, Add-Type, Clear-Variable, Compare-Object…}
Manifest   7.0.0.0            Microsoft.WSMan.Management            {Connect-WSMan, Disable-WSManCredSSP, Disconnect-WSMan, Enable-WSManCredSSP…}
Script     2.1.0              PSReadLine                            {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PSReadLineKeyHandler, Set-PSReadLineKeyHandler…}

PS C:\> Register-SecretVault -Name SecretStore -ModuleName Microsoft.PowerShell.SecretStore -DefaultVault
Register-SecretVault: Could not load and retrieve module information for module: Microsoft.PowerShell.SecretStore with error : The specified module 'Microsoft.PowerShell.SecretStore' was not loaded because no valid module file was found in any module directory..
PS C:\>
PS C:\>

Mtndrew11 avatar Feb 10 '22 15:02 Mtndrew11

I was able to circumvent the issue above by installing the module with the "-AllowPrerelease" parameter.

Is there another release and/or method I can install the prerelease version in an offline environment that cannot reach out to the PSGallery?

Mtndrew11 avatar Feb 10 '22 15:02 Mtndrew11

You should not need the -AllowPrerelease parameter as all latest versions of SecretManagement and SecretStore are full releases. I am wondering if you have older versions of the module still on your machine. Try running Get-Module *secret* -list and ensure that any older versions are removed and that you are only using the latest versions.

PaulHigin avatar Feb 10 '22 21:02 PaulHigin

Found the culprit! When I downloaded the .nupkg file so that I can use this module in an offline environment, I renamed the file extension to ".zip" and unpacked the contents. At that point, the folder name was "microsoft.powershell.secretmanagement.1.1.2" and all of the contents were inside of that folder. The proper folder structure when installing it via command line configures it as microsoft.powershell.secretmanagement\1.1.2\ContentsHere. Comparison listed below:

Incorrect folder structure: C:\Users\username\Documents\PowerShell\Modules\Microsoft.PowerShell.SecretManagement.1.1.2\Microsoft.PowerShell.SecretManagement.psd1

Correct folder structure: C:\Users\username\Documents\PowerShell\Modules\Microsoft.PowerShell.SecretManagement\1.1.2\Microsoft.PowerShell.SecretManagement.psd1

Mtndrew11 avatar Feb 11 '22 19:02 Mtndrew11