0

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

谢谢 Swapnil Gangrade

4

1 回答 1

1

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

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。

于 2013-04-08T12:24:04.510 回答