4

I have made a Electron Application, mainly for my helpdesk colleagues at work.

What it does (Context):

It provides a simple gui with different "Tasks" it can do. Example:

enter image description here

For this I'm using a combination of:

  • Node with Electron
  • React
  • Node-Powershell

The Problem:

The app when I run it in Development Mode is running as it should. But after I build and package it with electron, the NPM package node-powershell can't find the .ps1 scripts anymore. I think I already identified the problem, but I don't know what the solution to this problem is. node-powershell needs a path to a script and an array of commands. I have set it up like this:

    const p1 = require('path').resolve();
    const scriptPath = require('path').join(p1, 'src/PowershellScripts/AddMailboxPermissions.ps1');

And then ps.addCommand(scriptPath, commands); (node-powershell)

As said before this works fine in development mode with a webpack server, but when run after packaging I get this error:

C:\Users\Huberdo\Projekte\Powershell-SAS-App\Client\builds\basic-electron-react-boilerplate-win32-x64\src\PowershellScripts\GetRemoteMac.ps1 cannot be found.

The reason is because the packaging has following structure. The .ps1 files now have following path:

C:\Users\Huberdo\Projekte\Powershell-SAS-App\Client\builds\basic-electron-react-boilerplate-win32-x64\resources\app\src\PowershellScripts

So this is the main issue. How do I package this "the right way" so that it's actually working afterwards.

Infos:

  • I run this on a windows 10 x64 machine
  • package.json scripts:
    • "dev": "webpack-dev-server --hot --host 0.0.0.0 --config=./webpack.dev.config.js", "build": "webpack --config webpack.build.config.js", "package": "webpack --config webpack.build.config.js",
  • When I run it in dev mode it resolves the correct path and everything is working just fine

I hope someone of you can help me or guide me into the right direction.

Possible Idea:

Reference a completely other path and copy the ps1 files into another directory with a gulp task? Never used it. Could this be a possible solution? If yes how to best approach this?

Update When I copy the src folder from C:\Users\Huberdo\Projekte\Powershell-SAS-App\Client\builds\basic-electron-react-boilerplate-win32-x64\resources\app into the root C:\Users\Huberdo\Projekte\Powershell-SAS-App\Client\builds\basic-electron-react-boilerplate-win32-x64 it's working again. So it's mainly a path issue and I don't know how to solve it.

Last but not least a direct link to the project on github:

https://github.com/dhuber666/Powershell-SAS-Tool

Here I call the ps1 script:

https://github.com/dhuber666/Powershell-SAS-Tool/blob/SendAs/src/components/scriptsComponents/AddMailboxPermissions.js

!Thank you!

1 Answer 1

0

I am doing something similar, but the approach I am using for the PowerShell scripts is to create them as PowerShell modules. Then you can just place them in one of the valid PSModule location and you should be good to go. And you could just write a powershell script to copy you modules to the correct location and copy the package to the desktop or where ever it needs to be.

A very simple module could be something like this:

In C:\Program Files\WindowsPowerShell\Modules create a folder called MyModuleTest. In this folder create a file called MyModuleTest.psm1. In this PSM1 file create a function e.g.

function Invoke-MyModuleTest {

Write-host "This is My Module"

}

Save the file, open a powershell console session and then if you run Invoke-MyModuleTest it will output to the console.

You can read more about modules here

You can also find some nice examples of more complex modules here

Once your have converted your script to modules you can just call them like any cmdlet, I currently don't have anything I can share.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer. It sounds interesting. Could please further explain how you are doing this? Do you have an example or something like that on github? Thx
@GeraltDieSocke update the original response, hopefully it is more useful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.