views:

41

answers:

2

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 iis to provide CLI for custom tasks?

A: 

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.)

kbrimington
+1  A: 

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.

Keith Hill
This can call powershell commands, but i want to call my application from powershell. I am looking for information which can allow me to call actions in my application through Command line
mamu
Ah, then you should look into fan-in remoting in Powershell 2.0: http://blogs.msdn.com/b/powershell/archive/2009/04/10/configuring-powershell-for-remoting-part-2-fan-in.aspx
Keith Hill