Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell commands in Windows RT

I am developing an application in Windows RT.I do have an requirement to run/invoke PowerShell or CMD Commands in Windows RT(C#).Please help me to figure out. Thanks in advance

like image 583
sathia Avatar asked Dec 22 '25 16:12

sathia


1 Answers

While you can't program directly against the PowerShell .NET engine in Windows RT, you can access the desktop via a socket. However this presumes you have the ability to ensure the desktop socket listener is installed & running. However, with that in place & running you can issue commands to it and let it call cmd.exe (or powershell via the EXE or in-proc engine). What would be nice to have in WinRT are the engine pieces that allow you to connect to & use a PowerShell remoting endpoint. Consider that ultimately is just a socket, that could be done by someone outside Microsoft but you'd have to wade through a lot of the WS-MAN protocol to figure out how it works. I thinking a simple string sent over the socket might be easier e.g. 'powershell -command "& { Get-Process }"' or 'cmd /c dir c:\'. :-)

Here's some snippets of code as a proof of concept:

WinRT code snippet:

private async void _button_OnClick(object sender, RoutedEventArgs e)
{
    string content;
    try
    {
        var client = new HttpClient();
        client.DefaultRequestHeaders.Add("user-agent", 
            "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
        var response = await client.GetAsync("http://localhost:54649/");
        if (response.StatusCode == HttpStatusCode.OK)
        {
            content = await response.Content.ReadAsStringAsync();
        }
        else
        {
            content = response.StatusCode.ToString();
        }

    }
    catch (Exception ex)
    {
        content = ex.Message;
    }

    await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        _textBlock.Text = content;
    });
}

On the desktop side, here's a snippet of a brain dead MVC controller:

public class HomeController : Controller
{
    //
    // GET: /Home/
    public string Index()
    {
        return "Hello World. The time is " + DateTime.Now;
    }
}
like image 151
Keith Hill Avatar answered Dec 24 '25 07:12

Keith Hill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!