Skip to content
This repository was archived by the owner on Jun 22, 2026. It is now read-only.
This repository was archived by the owner on Jun 22, 2026. It is now read-only.

While developing an extension: Prevent session restart for reloading the module #206

Open

Description

Summary of the new feature / enhancement

I'm developing a (script based) extension for SecretManagement. Currently any code change needs a restart of the terminal. In earlier versions Register-SecretVault did a Import-Module -Force but I'm afraid that's gone with the new runspace design.

Expected Behavior

Register-SecretVault -Name MySafe -ModuleName $modulePath -AllowClobber
# Do some Changes in the code
Register-SecretVault -Name MySafe -ModuleName $modulePath -AllowClobber
# Changed code is executed

Actual behavior

Register-SecretVault -Name MySafe -ModuleName $modulePath -AllowClobber
# Do some Changes in the code
Register-SecretVault -Name MySafe -ModuleName $modulePath -AllowClobber
# Old code is executed

Proposed technical implementation details (optional)

No response

Activity

  1. llewellyn-marriott commented on Mar 15, 2023

    @llewellyn-marriott

    I ran into this issue also; it was making extension development very tedious. I used this quick hack at the start of my test script:

    # Get the runspace field
    $RunspaceField = (([Microsoft.PowerShell.SecretManagement.SecretVaultInfo].Assembly.GetTypes() | Where-Object Name -eq 'PowerShellInvoker').DeclaredFields | Where-Object Name -eq '_runspace')
    # Get current runspace value and dispose of it
    $RunspaceValue = $RunspaceField.GetValue($null)
    if($NULL -ne $RunspaceValue) {
        $RunspaceValue.Dispose()
    }
    # Set the runspace field to null
    $RunspaceField.SetValue($null, $null)

    This code will dispose the Runspace if it is set, and then set it to null. The Runspace will be recreated next time it is used.

    I suppose the fix would be to remove the module from the Runspace when Unregister-SecretVault is called. As long as there are no other vaults registered with the same module.

  2. Callidus2000 commented on Mar 15, 2023

    @Callidus2000
    Author

    Nice.
    I've started to use the module RestartableSession. It automatically restarts a new clean session with a lot of initialization options. The workaround is documented in my template module and in the corresponding ReadMe.

    I'll include your fix into my documentation. It's a lot faster than mine, but the RestartableSession approach has the benefit that it keeps other vaults alive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions