0

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

感谢 Swapnil Gangrade

回答

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作为错误的用户。

+0

提供链接的摘要,如果它死了,你的答案将变得毫无用处。 [请参阅](http://stackoverflow.com/help/how-to-answer) – 2013-09-30 20:28:38

+0

@JonathanDrapeau在这里你是 – 2013-10-01 06:25:54