0

Is there a way to find functions that were imported into an InitialSessionState using ImportPSModule()?

I have this wrapper around PowerShell to run scripts self-hosted by my applicaton (comments and error checking removed for brevity)

public class Engine
{
    private readonly InitialSessionState sessionState;

    public Engine(string initializationModulePath)
    {
        this.sessionState = InitialSessionState.CreateDefault();
        this.sessionState.LanguageMode = PSLanguageMode.FullLanguage;
        this.sessionState.AuthorizationManager = null;
        // New functionality - allow an initialization file.
        this.sessionState.ImportPSModule(new[] { initializationModulePath });
    }

    public void AddCommand(String name, Type implementingType) => this.sessionState.Commands.Add(
            new SessionStateCmdletEntry(
                name,
                implementingType,
                null));

    public void RunScript(string script, string scriptComment)
    {
        using (PowerShell powerShellCommand = PowerShell.Create())
        {
            powerShellCommand.AddScript(script);

            using (powerShellCommand.Runspace = RunspaceFactory.CreateRunspace(this.sessionState))
            {
                powerShellCommand.Runspace.Open();
                Collection<PSObject> results = powerShellCommand.Invoke();
                // Results processing omitted.
            }
        }
    }

    // Testing - I see things added via AddCommand() but not anything imported via ImportPSModule
    public void ListCommands()
    {
        foreach (SessionStateCommandEntry commandEntry in this.sessionState.Commands)
        {
            Console.WriteLine($"Command: {commandEntry.Name}, Type: {commandEntry.CommandType}");
        }
    }
}

When I add types using engine.AddCommand(), they show up in engine.ListCommands() as expected. I now want to allow users to have a custom set of functions they've predefine which I'll import via ImportPSModule(). Finally, I'd like to be able to list out those commands in the UI as commands that are available.

I created a really simple module (init.psm1)

function Add-Number {[CmdletBinding()] param ([int]$a, [int]$b) $a + $b }
function Subtract-Number {[CmdletBinding()] param ([int]$a, [int]$b) $a - $b }

and tested importing it, but the functions don't show up as available commands. (Though user scripts can use them just fine.)

I looked at System.Management.Automation.Language.Parser.ParseFile based on this question and could check for tokens after {function}, but it seems like a round-about way to do it. (Currently, this is the best option I have.)

I considered running a little script with Get-ChildItem function:, but Get-ChildItem is not available when using InitialSessionState.CreateDefault() (I do not want to use InitialSessionState.Create(). My scenario requires the engine to be more locked down than that.)

Is there another option I can use to get the list of user-defined functions? Importing them in another manner is acceptable so long as users can provide them as powershell code.

Thanks!

Edit Re-reading the documentation, it seems I had missed this very important line:

Add a list of modules to import when the runspace is created.

So I'll update the question a little to be "Is there an way to create a SessionStateCommandEntry() from a script file that can be added to initialSessionState.Commands?"

2
  • Try adding Export-Module -Function Add-Number,Subtract-Number to the psm1 file Commented Feb 14, 2021 at 22:46
  • Thanks. I gave that a shot but no luck. I've updated the question to reflect a key piece of documentation I missed (when the module is executed) and clarified that what I seem to need as a way to create a SessionStateCommandEntry() using a *.psm1 as the input. Commented Feb 15, 2021 at 9:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.