开发者

hosting powershell like cli in asp.net mvc to perform maintaince

I need CLI for our asp.net mvc application to perform maintenance tasks and some status tasks.

I think powershell is designed to provide CLI. But i do not know anything about it other than name.

How can i host powershell in asp.net mvc running in ii开发者_如何学运维s to provide CLI for custom tasks?


The .NET code to host and use the PowerShell engine is very simple:

private void ExecutePowerShellCommand(string command)
{
    using (var invoker = new RunspaceInvoke())
    {
        Collection<PSObject> results = invoker.Invoke(command);
        foreach (var result in results)
        {
            _listBox.Items.Add(result);
        }                
    }
}

The trick is in configuring the PowerShell runspace to limit the available commands. You probably don't want to allow someone to delete files from any old directory, shutdown the computer or format a drive (they would have access to EXEs in the path). Look into Constrained Runspaces to limit what can be executed via this mechanism. You can also limit which language features are available.


I've never done CLI within PowerShell before, but I have executed some PowerShell goodness within a .NET application before. Here's something that might help.

This Channel9 episode contains relevant details for running PowerShell 2.0 commands in a .NET application (which is a lot easier than 1.0): http://channel9.msdn.com/posts/bruceky/How-to-Embedding-PowerShell-Within-a-C-Application/

(Note that the process does not include using System.Diagnostics.Process to invoke the commands while manipulating the process' standard input and output stream. Any attempts I made down that road were met with frustration and failure.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜