PowerShell using existing session state to populate InitialSessionState

I am trying to call \ create runpace inside a class derived from PSCmdlet. Since PSCmdlet includes a default session state that contains shared data that I want to reuse in the workspace, I wonder if there is a programmatic way to convert the current sessionState to the InitialSessionState execution space?

If there is no such way, I do not quite understand why such information about the session state cannot be used in different scenarios. This is like starting a remote workspace. Can someone explain?

For instance,

namespace CustomCmdlet.Test
{
    [Cmdlet("Get", "Test1")]
    public class GetTest1 : PSCmdlet
    {
        protected override void ProcessRecord()
        { WriteObject("1"); }
    }

    [Cmdlet("Get", "Test2")]
    public class GetTest2 : PSCmdlet
    {
        protected override void ProcessRecord()
        {
         // instead of import the module dll using Runspace
         // InitialSessionState.ImportModule(new string[] {"CustomCmdlet.Test.dll"});
         // Runspace runspace = RunspaceFactory.CreateRunspace(InitialSessionState)

         // is it any way to import the PSCmdlet.SessionState into the InitialSessionState?
        }
    }

We use PowerShell 4.0, if relevant.

+3
source share
1 answer

, , , , , .

InitialSessionState, , . , Runspace.DefaultRunspace: http://msdn.microsoft.com/en-us/library/system.management.automation.runspaces.runspace.defaultrunspace(v=vs.85).aspx

, RunspacePool - InitialSessionState. , , , .

+3

All Articles