2

I am hosting powershell within my app and have set up a restricted runspacepool, which is basically empty (to the best of my knowledge).

public class MyPowerShell : IDisposable
{
    private RunspacePool _runspacePool;
    private PowerShell _shell;

    public MyPowerShell()
    {
        try
        {
            var initialSessionState = InitialSessionState.CreateRestricted(SessionCapabilities.RemoteServer);

            _runspacePool = RunspaceFactory.CreateRunspacePool(initialSessionState);

            _shell = PowerShell.Create();
            _shell.RunspacePool = _runspacePool;
            _shell.RunspacePool.Open();

            _shell.AddCommand("Import-Module").AddParameter("Assembly", Assembly.GetExecutingAssembly());
            _shell.Invoke();
            _shell.Commands.Clear();
        }
        catch (Exception ex)
        {
            throw;
        }
    }

    public void Dispose()
    {
        _shell.RunspacePool.Close();
        _shell.Dispose();
    }

    public string[] Exec(string commandText)
    {
        var results = new List<string>();

        try
        {
            _shell.AddScript(commandText);
            foreach (var str in _shell.AddCommand("Out-String").Invoke<string>())
            {
                results.Add(str);
            }
        }
        catch (Exception ex)
        {
            results.Add(ex.Message);
        }
        return results.ToArray();
    }

}

obviously, when I run this code ...

        _shell.AddCommand("Import-Module").AddParameter("Assembly", Assembly.GetExecutingAssembly());
        _shell.Invoke();
        _shell.Commands.Clear();

it fails because there is no "Import-Module" cmdlet available. So, my question is how can I import a module without the "Import-Module" cmdlet being available?

This is the error message I am getting ...

The term 'Import-Module' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Antony Scott
  • 21,690
  • 12
  • 62
  • 94
  • Does x0n's answer help? http://stackoverflow.com/questions/6266108/powershell-how-to-import-module-in-a-runspace – Andy Arismendi Feb 08 '12 at 08:03
  • it shows an alternative way of importing modules, but I am still getting the same error (I'll update my question with the actual error text). thanks anyway. – Antony Scott Feb 08 '12 at 09:08
  • Just a silly question but you do have PS v2 installed right? Does your code work with other cmdlets such as `Get-Process`? – Andy Arismendi Feb 08 '12 at 09:40
  • Yes, I've got v2 installed, and i've tried creating a normal runspace and get execute "Get-Process" – Antony Scott Feb 08 '12 at 09:46

1 Answers1

3

I have found a solution, can't remember where I did now and I'd actually forgotten I'd asked this question! Anyway, here it is for the benefit of anyone with the same problem.

var initialSessionState = InitialSessionState.CreateRestricted(SessionCapabilities.RemoteServer);
initialSessionState.ImportPSModule(new [] { "ModuleName1", "ModuleName2" });

I can't actually see which module I was trying to import when I asked this question, but I have successfully used the above code for using AppFabric administration powershell cmdlets.

Antony Scott
  • 21,690
  • 12
  • 62
  • 94