PowerShell / SecretManagement

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

TypeInitializationException #89

Closed SamLue closed 2 years ago

SamLue commented 3 years ago

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

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

issue-label-bot[bot] commented 3 years ago

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

SydneyhSmith commented 3 years ago

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

SydneyhSmith commented 3 years ago

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

SamLue commented 3 years ago

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 commented 3 years ago

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.

craftzneko commented 3 years ago

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?

meestark commented 3 years ago

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

SamLue commented 3 years ago

@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.

pierodamafalda commented 3 years ago

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

iRon7 commented 2 years ago

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 commented 2 years ago

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.

kieranwalsh commented 2 years ago

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

montereyharris commented 2 years ago

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 commented 2 years ago

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

PaulHigin commented 2 years ago

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 commented 2 years ago

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

Mtndrew11 commented 2 years ago

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 commented 2 years ago

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?

PaulHigin commented 2 years ago

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.

Mtndrew11 commented 2 years ago

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