【问题标题】:how to integrate powershell with ASP.net web page如何将 powershell 与 ASP.net 网页集成
【发布时间】:2013-03-30 12:30:55
【问题描述】:

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

谢谢 斯瓦普尼尔·甘格莱德

【问题讨论】:

    标签: asp.net soap exchange-server powershell-2.0 powershell-3.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。

    【讨论】:

    • 提供链接的摘要,如果它死了,你的答案将变得毫无用处。 Please see
    • @JonathanDrapeau 你在这里
    猜你喜欢
    • 1970-01-01
    • 2014-07-18
    • 2012-01-27
    • 2020-02-24
    • 2013-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-12
    相关资源
    最近更新 更多