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