PowerShell / SecretManagement

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

Get-SecretInfo: The specified module '...\Microsoft.PowerShell.SecretStore' was not loaded because no valid module file was found in any module directory. #217

Open rkeithhill-keysight opened 10 months ago

rkeithhill-keysight commented 10 months ago

Prerequisites

Steps to reproduce

I'm not sure how to reproduce this since this only happens on my work PC which has redirected my ~\Document\PowerShell folder to a corporate OneDrive folder. SecretManagement/Store used to not give this error before my company forced this redirection. Note that the command still works but it is annoying getting this error spit out every time. Basically, all I have to do to get this error is:

  1. Start new PowerShell session
  2. Run Get-SecretInfo

I've tried preloading the modules and I still get this error:

image

The preloaded modules were loading from:

 > Get-Module *Secret* | Format-Table Name,Path  -AutoSize

Name                                  Path
----                                  ----
Microsoft.PowerShell.SecretManagement C:\Users\hillr\OneDrive - Keysight Technologies\Documents\PowerShell\Modules\Microsoft.PowerShell.SecretManagement\1.1.2\Microsoft.PowerShell.SecretManagement.dll
Microsoft.PowerShell.SecretStore      C:\Users\hillr\OneDrive - Keysight Technologies\Documents\PowerShell\Modules\Microsoft.PowerShell.SecretStore\1.0.6\Microsoft.PowerShell.SecretStore.dll

Finally, my $env:PSModulePath is set to:

> $env:PSModulePath -split ';'
C:\Users\hillr\OneDrive - Keysight Technologies\Documents\PowerShell\Modules
C:\Program Files\PowerShell\Modules
c:\program files\powershell\7\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
C:\Program Files\Thycotic\Powershell\

I do not understand why there is an attempt to load from C:\Users\hillr\Documents\PowerShell\Modules\.

Expected behavior

I should see no error about the module file not being found.

Actual behavior

> Get-SecretInfo
Get-SecretInfo: The specified module 'C:\Users\hillr\Documents\PowerShell\Modules\Microsoft.PowerShell.SecretStore' was not loaded because no valid module file was found in any module directory.
Vault PSUSecretStore requires a password.
Enter password:
***********

Name              Type         VaultName
----              ----         ---------
ApiDocsKey        String       PSUSecretStore

Error details

Exception             : 
    Type    : System.IO.FileNotFoundException
    Message : The specified module 'C:\Users\hillr\Documents\PowerShell\Modules\Microsoft.PowerShell.SecretStore' was not loaded because no valid module file was found in any module directory.
    HResult : -2147024894
TargetObject          : C:\Users\hillr\Documents\PowerShell\Modules\Microsoft.PowerShell.SecretStore
CategoryInfo          : ResourceUnavailable: (C:\Users\hillr\Docu…erShell.SecretStore:String) [Get-SecretInfo], FileNotFoundException
FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.SecretManagement.GetSecretInfoCommand
InvocationInfo        : 
    MyCommand        : Get-SecretInfo
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 3
    Line             : Get-SecretInfo
    Statement        : Get-SecretInfo
    PositionMessage  : At line:1 char:1
                       + Get-SecretInfo
                       + ~~~~~~~~~~~~~~
    InvocationName   : Get-SecretInfo
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 13
PipelineIterationInfo :

Environment data

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

Version

1.1.2

Visuals

No response

rkeithhill-keysight commented 10 months ago

Hmm,

 > Get-SecretVault | fl *

Name            : BuiltInLocalVault
ModuleName      : SecretManagement.JustinGrote.CredMan
ModulePath      : C:\Program Files (x86)\Universal\Modules\SecretManagement.JustinGrote.CredMan
Description     :
VaultParameters : {}
IsDefault       : False

Name            : LocalStore
ModuleName      : Microsoft.PowerShell.SecretStore
ModulePath      : C:\Users\hillr\Documents\PowerShell\Modules\Microsoft.PowerShell.SecretStore
Description     :
VaultParameters : {}
IsDefault       : True

Name            : PSUSecretStore
ModuleName      : Microsoft.PowerShell.SecretStore
ModulePath      : C:\Users\hillr\OneDrive - Keysight
                  Technologies\Documents\PowerShell\Modules\Microsoft.PowerShell.SecretStore
Description     :
VaultParameters : {}
IsDefault       : False
rkeithhill-keysight commented 10 months ago

OK, edited this file "$HOME\AppData\Local\Microsoft\PowerShell\secretmanagement\secretvaultregistry\vaultinfo" to fix the bogus old (pre-OneDrive migration) path and now the annoying error is gone.

QS8 commented 9 months ago

Thank you rkeithhill. I was receiving the same error. I had looked around for a few hours for the file which held the string to that path which was being used but which was no longer correct to reach the SecretStore module. And the "vaultinfo" file was where the data was.

It seems that, in your case and mine, the problem with the path to the SecretStore module occurred because, at some time point after installation of the Microsoft SecretStore, the path to the special \Documents\ (aka "MyDocuments") folder in Windows was changed, and the contents of the \Documents\ folder moved to the new location. So, the folder which contains the SecretStore module was moved to that new location.

And for the SecretStore, Microsoft had used an algorithm such that the path listed in the "vaultinfo" file contains a string which pointed to the location of the Documents special folder at the time of installation of the Microsoft SecretStore.

So, I suggest that, in the next version of SecretStore, Microsoft change the algorithm so that the path listed in the "vaultinfo" file will contain a string which points to the Documents special folder. That way the path can be found at runtime, rather than at installation of the SecretStore module.

rkeithhill-keysight commented 9 months ago

Also, I'm hoping a future version of PowerShell moves modules out of Documents\PowerShell\Modules into a $HOME\AppData\Local folder. That would have prevented this problem from occurring.