Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8333875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:09:59+00:00 2026-06-09T03:09:59+00:00

I am hosting powershell within my app and have set up a restricted runspacepool,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:10:01+00:00Added an answer on June 9, 2026 at 3:10 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When hosting WPF user controls within a WinForms MDI app there is a drawing
My hosting sent me email saying I have set the session.save_path = /home/users/web/b1475/moo.youraccount/cgi-bin/tmp for
i have a hosting server on godaddy. i have the economic plan which is
I use shared hosting so I can't access to shell. I have CronsController and
I'm hosting a Yii app on shared-host with some my friend, and keep database
I have godaddy hosting and I am using it to host multiple domains. With
My hosting service just informed me that they don't have Git installed on any
My hosting provider does not have curl extension enabled by default, however, I can
I am hosting a SVN repository and I want to set up a hook
I just started hosting my app on a new domain. I selected a url

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.