使用自定义 cmdlet 托管受限制的 Powershell

发布于 2025-01-03 01:26:06 字数 1915 浏览 1 评论 0原文

我在我的应用程序中托管 powershell,并设置了一个受限的运行空间池,该池基本上是空的(据我所知)。

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();
    }

}

显然,当我运行此代码时...

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

它失败,因为没有可用的“Import-Module”cmdlet。所以,我的问题是如何在“Import-Module”cmdlet 不可用的情况下导入模块?

这是我收到的错误消息...

术语“Import-Module”未被识别为 cmdlet 的名称, 函数、脚本文件或可运行程序。检查拼写 名称,或者如果包含路径,请验证路径是否正确并且 再试一次。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹倦 2025-01-10 01:26:06

我找到了解决方案,不记得我现在在哪里做了,而且我实际上忘记了我问过这个问题!无论如何,这是为了有同样问题的人的利益。

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

当我问这个问题时,我实际上无法看到我正在尝试导入哪个模块,但我已成功使用上述代码来使用 AppFabric 管理 powershell cmdlet。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文