问题描述
我正在尝试从VB.Net内部运行一些PowerShell代码(如果您知道,C#编码器也可能会提供帮助!).
代码的第一部分,我需要使用密码连接到na-controller,并且需要保持连接打开.我还需要单击一个按钮来运行其他命令(获取文件,显示文件,删除特定的文件).
如何保持运行空间打开并分别运行每一行代码?我需要使用"CreateRunspacePool"吗?如果可以,该如何使用?
I'm trying to run some PowerShell code from within VB.Net (C# coders may also help if you know!).
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. 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"? If so, how do I use it?
我现在正在使用RunspaceStateInfo.State,但是我不知道保持连接打开的方法.我已经尝试过将Page_Load和Page_Init事件设置为仅打开一次,但沿行将其关闭.我确实删除了".Close"命令
I'm playing around now with the RunspaceStateInfo.State, but I don't know of a way to keep the connection open. I've tried the Page_Load and the Page_Init events to just have it open once, but somewhere along the line, it closes. I did remove the ".Close" command
推荐答案
您没有提供很多信息,但是从您谈论Page_Load和Page_Init的角度来看,我假设您正在尝试通过ASP.NET Web应用程序执行此操作.通过按下按钮并在运行空间上设置状态.
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. By pressing buttons and setting state on the Runspace.
如果这是一个winforms应用程序,则可以简单地创建一个运行空间:
If this is a winforms application you can simply create a runspace:
Runspace runspace = RunspaceFactory.CreateRunspace(Host);
然后创建powershell实例并重复使用该运行空间:
And then create powershell instances and just reuse this runspace:
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.
如果您位于asp.net中,但情况有所不同,显然,每次单击按钮都会产生一个新线程,并且您将无法将运行空间保留在变量中,因此您需要执行诸如keep的操作在会话中如下所示:
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:
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();
}
当我单击按钮1,然后单击按钮3时,它显示你好",并且
When I click button 1 and then 3 it displays "Hello" and
当我单击按钮2,然后单击按钮3时,它显示世界"
When I click button 2 and then 3 it displays "World"
因此它成功地重用了运行空间.
So it sucesfully reused the runspace.
这篇关于在.Net中保持Powershell运行空间打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!