как интегрировать powershell с веб-страницей ASP.net

как интегрировать powershell с веб-страницей ASP.net, чтобы в любое время щелкнуть кнопку страницы asp.net. Powershell на удаленном сервере обмена выполнится и вернет результат. Также этот результат должен быть отправлен обратно на страницу asp.net для отображения на веб-странице пользователю. не могли бы вы помочь в этом.

Спасибо Свапнил Ганграде


person Swapnil Gangrade    schedule 08.04.2013    source источник


Ответы (1)


Посмотрите статью проекта кода о запуске powershell из 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 от имени другого пользователя.

person Piotr Stapp    schedule 08.04.2013