.net 中的持久运行空间 (C#)
Persistent Runspace in .net (C#)
我想在一些网页代码中创建一个 运行space,一个 power shell 会话将 运行 在其中。我想要这个 运行 space 在用户打开页面时创建,并保持打开状态直到页面关闭。因此,例如下面的代码是单击一个按钮。但每次我 运行 一个电源 shell 命令我需要 运行
"$S = new-pssesession -configurationname micros......"
这需要几秒钟,只是设置会话,我在哪里可以创建 运行space 和 powershell 对象,以便我可以从任何地方调用它每次我想使用它时都不必重新创建代码。在控制台应用程序中,我会在 "main" class 中噘嘴,但似乎网络 pages/applications 具有不同的结构
protected void Button1_Click(object sender, EventArgs e)
{
var runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
var powershell = PowerShell.Create();
//{
powershell.Runspace = runspace;
powershell.AddScript("Set-ExecutionPolicy Unrestricted ; $s=new-pssession -configurationname microsoft.exchange -connectionuri http://server.com/powershell -authentication kerberos ; import-pssession $s");
powershell.Invoke();
powershell.AddScript("get-dynamicdistributiongroup");
Collection<PSObject> resultsL = powershell.Invoke();
// close the runspace
runspace.Close();
Input.Items.Clear();
foreach (var dlist in resultsL)
{
Input.Items.Add(dlist.ToString());
}
Input.DataBind();
我提出的解决方案
所以我想我会尝试构建一个静态的对象,我可以调用它。我认为通过创建一个静态调用,我第一次调用它时会 运行 "static powers()" 构造,但任何进一步的调用只会调用方法中的代码部分。但是不确定我是否完全正确。
public static class powers
{
static public Runspace runspace = RunspaceFactory.CreateRunspace();
static public PowerShell powershell = PowerShell.Create();
static powers()
{
runspace.Open();
powershell.Runspace = runspace;
powershell.AddScript("Set-ExecutionPolicy Unrestricted ; $s=new-pssession -configurationname microsoft.exchange -connectionuri http://exchangeserver.com/powershell -authentication kerberos ; import-pssession $s");
var resultL = powershell.Invoke();
}
static public Collection<PSObject> glist()
{
powershell.AddScript("get-dynamicdistributiongroup");
Collection<PSObject> resultL = powershell.Invoke();
return resultL;
}
static public Collection<PSObject> gmembers(string listn)
{
string GetMembers = "$FTE = Get-DynamicDistributionGroup '" + listn + "'";
powershell.AddScript(GetMembers);
powershell.Invoke();
powershell.AddScript("Get-Recipient -RecipientPreviewFilter $FTE.RecipientFilter");
Collection<PSObject> resultM = powershell.Invoke();
return resultM;
}
}
我已经构建了其他几个应用程序,我在会话中存储了一些东西并使用属性来访问它们。与此类似的内容可能对您有用。
public static Runspace runspace
{
get
{
//if not exist, create, open and store
if (Session["runspace"] == null){
Runspace rs = RunspaceFactory.CreateRunspace();
rs.Open();
Session["runspace"] = rs;
}
//return from session
return (Runspace)Session["runspace"];
}
}
然后您可以在事件处理程序中将其作为静态 属性 简单地访问。
我想在一些网页代码中创建一个 运行space,一个 power shell 会话将 运行 在其中。我想要这个 运行 space 在用户打开页面时创建,并保持打开状态直到页面关闭。因此,例如下面的代码是单击一个按钮。但每次我 运行 一个电源 shell 命令我需要 运行
"$S = new-pssesession -configurationname micros......"
这需要几秒钟,只是设置会话,我在哪里可以创建 运行space 和 powershell 对象,以便我可以从任何地方调用它每次我想使用它时都不必重新创建代码。在控制台应用程序中,我会在 "main" class 中噘嘴,但似乎网络 pages/applications 具有不同的结构
protected void Button1_Click(object sender, EventArgs e)
{
var runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
var powershell = PowerShell.Create();
//{
powershell.Runspace = runspace;
powershell.AddScript("Set-ExecutionPolicy Unrestricted ; $s=new-pssession -configurationname microsoft.exchange -connectionuri http://server.com/powershell -authentication kerberos ; import-pssession $s");
powershell.Invoke();
powershell.AddScript("get-dynamicdistributiongroup");
Collection<PSObject> resultsL = powershell.Invoke();
// close the runspace
runspace.Close();
Input.Items.Clear();
foreach (var dlist in resultsL)
{
Input.Items.Add(dlist.ToString());
}
Input.DataBind();
我提出的解决方案
所以我想我会尝试构建一个静态的对象,我可以调用它。我认为通过创建一个静态调用,我第一次调用它时会 运行 "static powers()" 构造,但任何进一步的调用只会调用方法中的代码部分。但是不确定我是否完全正确。
public static class powers
{
static public Runspace runspace = RunspaceFactory.CreateRunspace();
static public PowerShell powershell = PowerShell.Create();
static powers()
{
runspace.Open();
powershell.Runspace = runspace;
powershell.AddScript("Set-ExecutionPolicy Unrestricted ; $s=new-pssession -configurationname microsoft.exchange -connectionuri http://exchangeserver.com/powershell -authentication kerberos ; import-pssession $s");
var resultL = powershell.Invoke();
}
static public Collection<PSObject> glist()
{
powershell.AddScript("get-dynamicdistributiongroup");
Collection<PSObject> resultL = powershell.Invoke();
return resultL;
}
static public Collection<PSObject> gmembers(string listn)
{
string GetMembers = "$FTE = Get-DynamicDistributionGroup '" + listn + "'";
powershell.AddScript(GetMembers);
powershell.Invoke();
powershell.AddScript("Get-Recipient -RecipientPreviewFilter $FTE.RecipientFilter");
Collection<PSObject> resultM = powershell.Invoke();
return resultM;
}
}
我已经构建了其他几个应用程序,我在会话中存储了一些东西并使用属性来访问它们。与此类似的内容可能对您有用。
public static Runspace runspace
{
get
{
//if not exist, create, open and store
if (Session["runspace"] == null){
Runspace rs = RunspaceFactory.CreateRunspace();
rs.Open();
Session["runspace"] = rs;
}
//return from session
return (Runspace)Session["runspace"];
}
}
然后您可以在事件处理程序中将其作为静态 属性 简单地访问。