如何将PowerShell与ASP.net网页集成

时间:2013-04-08 12:19:55

标签: asp.net soap exchange-server powershell-v2.0 powershell-v3.0

如何将PowerShell与ASP.net网页集成,以便任何时候任何人点击asp.net页面按钮。远程交换服务器上的powershell将执行并返回结果。此结果也必须发送回asp.net页面以显示在用户的网页上。你能帮忙吗?

由于 Swapnil Gangrade

1 个答案:

答案 0 :(得分:1)

查看有关running powershell from C#的代码项目文章。示例代码如下:

private string RunScript(string scriptText)
{
    // create Powershell runspace

    Runspace runspace = RunspaceFactory.CreateRunspace();

    // open it

    runspace.Open();

    // create a pipeline and feed it the script text

    Pipeline pipeline = runspace.CreatePipeline();
    pipeline.Commands.AddScript(scriptText);

    // add an extra command to transform the script
    // output objects into nicely formatted strings

    // remove this line to get the actual objects
    // that the script returns. For example, the script

    // "Get-Process" returns a collection
    // of System.Diagnostics.Process instances.

    pipeline.Commands.Add("Out-String");

    // execute the script

    Collection<psobject /> results = pipeline.Invoke();

    // close the runspace

    runspace.Close();

    // convert the script result into a single string

    StringBuilder stringBuilder = new StringBuilder();
    foreach (PSObject obj in results)
    {
        stringBuilder.AppendLine(obj.ToString());
    }

    return stringBuilder.ToString();
}

但要小心模仿,因为你可以将这个powershell作为错误的用户运行。