0

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

Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
maximdj
  • 315
  • 1
  • 12

2 Answers2

1

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/

Stefan Wick MSFT
  • 13,600
  • 1
  • 32
  • 51
  • Would you be able to give me an example of how to create a simple exe that would serve this purpose? I have put powershell.exe in my package folder which opens when the app runs but obviously doesn't work how I would like it to. I would like to run different commands using the groupID parameters which I do not know if is possible or not. – maximdj Jun 25 '18 at 21:15
  • Just to add that this app is not intended for the store, it is purely for my desktop purposes. – maximdj Jun 25 '18 at 21:32
  • I have created an .exe file with system("start powershell.exe"); and powershell opens but how can i pass the command i would like powershell to run? Thanks – maximdj Jun 25 '18 at 21:45
  • Updated to my answer to explain this – Stefan Wick MSFT Jun 25 '18 at 22:21
  • Thanks, I have come across that article many times but cannot make sense of it. I created an .exe in c++ console which has int main() And trying to pass the parameter into the int main(). – maximdj Jun 25 '18 at 22:48
  • Can't do it that way. Instead, you need to write the parameters to AppData.LocalSettings in the UWP. Then your console EXE can read the parameters from there and pass it into the powershell command. – Stefan Wick MSFT Jun 25 '18 at 23:00
  • The bit that I am confused with is the part about win32 side. I created a win32.exe in c++. – maximdj Jun 25 '18 at 23:15
  • how do I retrieve ApplicationData.Current.LocalSettings.Values["parameters"] = commandID; value from my win32 console.exe? Thanks – maximdj Jun 25 '18 at 23:35
  • Same way as in C#, just with C++ syntax :-) - All WinRT APIs are accessible from C++ as well as C# (and JavaScript). Here is an example from an unrelated sample: https://github.com/Microsoft/Windows-universal-samples/blob/6370138b150ca8a34ff86de376ab6408c5587f5d/Samples/ContentIndexer/cpp/S4_CheckIndexRevision.xaml.cpp – Stefan Wick MSFT Jun 25 '18 at 23:39
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/173777/discussion-between-maximdj-and-stefan-wick-msft). – maximdj Jun 25 '18 at 23:50
0

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

maximdj
  • 315
  • 1
  • 12