简体   繁体   English

在 .Net 中保持 Powershell 运行空间打开

[英]Keeping Powershell runspace open in .Net

I'm trying to run some PowerShell code from within VB.Net (C# coders may also help if you know!).我正在尝试从 VB.Net 中运行一些 PowerShell 代码(如果您知道,C# 编码人员也可能会有所帮助!)。
The first part of the code, I need to connect to a na-controller with a password and I need to keep the connection open.代码的第一部分,我需要使用密码连接到 na-controller 并且我需要保持连接打开。 I have other commands I need to run with a click of a button (get files, show files, delete specific ones).我还有其他命令需要单击按钮来运行(获取文件、显示文件、删除特定文件)。
How can I keep the runspace open and run each line of code separately?如何保持运行空间打开并分别运行每一行代码? Do I need to use the "CreateRunspacePool"?我需要使用“CreateRunspacePool”吗? If so, how do I use it?如果是这样,我该如何使用它?

Edit: I'm playing around now with the RunspaceStateInfo.State, but I don't know of a way to keep the connection open.编辑:我现在正在使用 RunspaceStateInfo.State,但我不知道保持连接打开的方法。 I've tried the Page_Load and the Page_Init events to just have it open once, but somewhere along the line, it closes.我已经尝试过 Page_Load 和 Page_Init 事件让它打开一次,但在某处,它关闭了。 I did remove the ".Close" command我确实删除了“.Close”命令

You have not provided a lot of info but from you talking about the Page_Load and Page_Init I am assuming you are trying to do this from an ASP.NET web application.您没有提供很多信息,但是从您谈论 Page_Load 和 Page_Init 我假设您正在尝试从 ASP.NET Web 应用程序执行此操作。 By pressing buttons and setting state on the Runspace.通过按下按钮并在 Runspace 上设置状态。

If this is a winforms application you can simply create a runspace:如果这是一个 winforms 应用程序,您可以简单地创建一个运行空间:

Runspace runspace = RunspaceFactory.CreateRunspace(Host);

And then create powershell instances and just reuse this runspace:然后创建 powershell 实例并重用此运行空间:

var ps1 = PowerShell.Create();
ps1.Runspace = runspace;
ps1.AddScript("Write-Host 'hello'")

var ps2 = PowerShell.Create();
ps2.Runspace = runspace;
ps2.AddScript("Write-Host 'world'")

You can keep the runspace around and simply run scripts between button clicks against that same runspace.您可以保留运行空间,并在针对同一运行空间的按钮单击之间简单地运行脚本。

If you are in asp.net however this is different, obviously each time you click a button a new thread is spawned and you will not be able to keep the runspace in a variable, so you would need to do something like keep it around in the session as follows:如果您在 asp.net 中,但是这是不同的,显然每次单击按钮时都会产生一个新线程,并且您将无法将运行空间保留在变量中,因此您需要执行诸如将其保留在会议如下:

protected PSHost Host
{
    get
    {

        if (this.Session["Host"] == null)
        {
            var host = new MyHost();
            this.Session["Host"] = host;
        }

        return (PSHost)this.Session["Host"];
    }
}


protected Runspace Runspace
{
    get
    {

        if (this.Session["Runspace"] == null)
        {
            var rs = RunspaceFactory.CreateRunspace(Host);
            this.Session["Runspace"] = rs;
            rs.Open();
        }

        return (Runspace)this.Session["Runspace"];
    }
}

And then I tested that this works:然后我测试了这是否有效:

protected void Page_Load(object sender, EventArgs e)
{
    Button1.Click += new EventHandler(Button1_Click);
    Button2.Click += new EventHandler(Button2_Click);
    Button3.Click += new EventHandler(Button3_Click);
}

void Button3_Click(object sender, EventArgs e)
{
    var ps = PowerShell.Create();
    ps.Runspace = this.Runspace;
    ps.AddScript("$test | ft | out-string");
    var input = new List<object>();
    var output = new List<object>();
    ps.Invoke(input, output);
    TextBox1.Text = output.First().ToString();
}

void Button2_Click(object sender, EventArgs e)
{
    var ps = PowerShell.Create();
    ps.Runspace = this.Runspace;
    ps.AddScript("$test = 'world'");
    ps.Invoke();
}

void Button1_Click(object sender, EventArgs e)
{
    var ps = PowerShell.Create();
    ps.Runspace = this.Runspace;
    ps.AddScript("$test = 'hello'");
    ps.Invoke();
}

When I click button 1 and then 3 it displays "Hello" and当我单击按钮 1 然后单击按钮 3 时,它显示“你好”和

When I click button 2 and then 3 it displays "World"当我单击按钮 2 然后单击按钮 3 时,它显示“世界”

So it sucesfully reused the runspace.所以它成功地重用了运行空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM