maximdj
maximdj

Reputation: 355

how to execute a powershell command using FulltrustProcessLauncher

I am trying to launch a simple command using powershell within a UWP app. I am hitting a brick wall and have done the following:

In my Package Manifest file I have put the following:

<Extensions>
    <desktop:Extension Category="windows.fullTrustProcess" Executable="powershell.exe">

I then created a group id:

<desktop:ParameterGroup GroupId="RestartPC" Parameters="Restart-Computer"/>

In my MainPage.xaml.cs I put the following:

string commandID = "RestartPC";

        IAsyncAction operation = FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync(commandID);

When I launch the app , the powershell window pops up for a second and then closes but nothing happens.

Any help would be appreciated. I looked at this thread for clues but it does not seem to make any difference.

UWP and FullTrustProcessLauncher missing in namaspeace

Upvotes: 0

Views: 1367

Answers (2)

maximdj
maximdj

Reputation: 355

Finally got there.

In the App Manifest file I put this:

<Extensions>
    <desktop:Extension Category="windows.fullTrustProcess" Executable="ConsoleApp1.exe">
  <desktop:FullTrustProcess>
        <desktop:ParameterGroup GroupId="RestartPC" Parameters="c:\mypath\Powershelldb.exe"/>
    <desktop:ParameterGroup GroupId="db" Parameters="c:\mypath\shutdowndb.exe"/>

      </desktop:FullTrustProcess>

I then created a console c++ application that has the following code:

using System;
using System.IO;
using System.Diagnostics;
using System.Reflection;
class Program
{
static void Main(string[] args)
{
    try
    {

        if (args.Length != 0)
        {
            string executable = args[2];

            string path=Assembly.GetExecutingAssembly().CodeBase;
            string directory=Path.GetDirectoryName(path);
            Process.Start(directory+"\\"+executable);
            Process.Start(executable);
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
        Console.ReadLine();
    }

}

}

I then created the executable file I wanted to run as below:

#include "stdafx.h"
#include <iostream>


int main()
{
     system("start powershell.exe -command Restart-Computer");
     return 0;
}

In my UWP app I then used the following:

private async void doSomething()
    {


        string commandID = "db";

        ApplicationData.Current.LocalSettings.Values["parameters"] = commandID;

        await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync(commandID);


    }

Powershell then launched and shutdown my computer. Obviously this isn't the purpose of all this but I wanted to test powershell commands :)

I found a useful thread here which helped a lot.

Thanks Stefan for his useful input also

Upvotes: 0

Stefan Wick  MSFT
Stefan Wick MSFT

Reputation: 13850

The EXE specified in the 'fullTrustProcess' extension needs to be an EXE in your package. So the solution for your scenario is to include a simple EXE in your package that then just calls Process.Start() or ShellExecute() to launch the powershell.

To pass in arbitrary parameters from the UWP to the powershell, you can write them to local AppData and have the helper EXE pick it up from there. Here is an example of how to do this on my blog: https://stefanwick.com/2018/04/06/uwp-with-desktop-extension-part-2/

Upvotes: 1

Related Questions