Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage with webpack #49

Closed
jackdh opened this issue Apr 18, 2018 · 12 comments
Closed

Usage with webpack #49

jackdh opened this issue Apr 18, 2018 · 12 comments

Comments

@jackdh
Copy link

jackdh commented Apr 18, 2018

Hey,

So I've used webpack "file-loader" to load my powershell,

http://localhost:1212/dist/9d67ce14b061421ea6486929162ddaee.ps1

with

import powershell with "../../example/powershell.ps1"

then attempt to use it with

let ps = new Powershell({
      executionPolicy: 'Bypass',
      noProfile: true
    });

    ps.addCommand(powershell, [{ GigaWatts: 1.0 }]);

I get

is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

I'm guessing I'll need to read in the file then paste it as string into the command to bypass this but it would be great if it supported it.

@BenjaminMichael
Copy link

PowerShell runs the external .ps1 files not node-powershell, and so I'm not sure exactly what you are using Webpack (dev server?) for but it shouldn't make a big difference. There is no need to import the external script because PowerShell runs it with no help from Javascript.

Here is a simple example. Note that x could be anything and the params could be anything. You could be generating the PowerShell script dynamically or maybe you have a huge archive of them or you just have 1 script it doesn't matter because running external scripts is fundamental for PowerShell.

#when I have an external PowerShell file psexample.ps1
param (
    [Parameter (Mandatory=$False,Position=0)]
    [String]$myParam
    )
$out =  "[{message: $($myParam)}]"
$out | convertto-JSON | Out-Host
const shell = require('node-powershell');

let ps = new shell({
  executionPolicy: 'Bypass',
  noProfile: true
});

let x = "./psexample.ps1",
params = [{myParam: 'Banana'}]

ps.addCommand(x, params)
ps.invoke()
.then(output => {
  console.log(output);
  ps.dispose();
})
.catch(err => {
  console.log(err);
  ps.dispose();
});

This will result in:

NPS>  Process 16036 started

NPS>  Command invoke started
NPS>  ./psexample.ps1 -myParam Banana
NPS>  Command invoke finished

"[{message: Banana}]"

NPS>  Process 16036 exited with code 0

@BenjaminMichael
Copy link

BenjaminMichael commented Apr 18, 2018

For complex scripting I reccomend you write out the parameters yourself using template literals and pass it as a single string.

let scriptY = "./psexample.ps1",
paramX = "banana"
ps.addCommand(`${scriptY} -myParam '${paramX}' )
ps.invoke()

@jackdh
Copy link
Author

jackdh commented Apr 18, 2018

Hey thanks for the quick reply. Sorry I realized i'd made some spelling mistakes in the original question.

So I'm using your plugin within Electron and would like to be able to package the powershell scripts within the application its self.

Currently I'm attempting to get webpack to package these scripts up (which does work with the file-loader, if I visit that url in my original question I can see the output on the .ps1 file displayed) so that they are pulled in with the buillt application. I'm using the Electron-React-Boilerplate if that helps.

Does that make anymore sense?

@BenjaminMichael
Copy link

This issue has been raised and closed see #36

I use electron and I just make a zip file out of my project with my scripts included in the zip file. This is really a "problem" for PowerShell that you want it to execute precompiled scripts in non human readable format. Also note that asar electron apps can be unpacked extremely easily so you cant hide secrets in there.

@jackdh
Copy link
Author

jackdh commented Apr 18, 2018

Ah I see, Alright well none of those solutions are ideal so we'll keep looking. For now we will use your suggestion of adding them line by line within the code.

Thanks,

I'll leave this open for now as I'll come back when we have our solution worked out!

@BenjaminMichael
Copy link

What is the problem you are trying to solve by obscuring the .ps1 files inside an asar?

@BenjaminMichael
Copy link

Note that hashing is the appropriate way to store passwords. You can use a library for example bcrypt
https://www.npmjs.com/package/bcrypt

@jackdh
Copy link
Author

jackdh commented Apr 18, 2018

So I'm not trying to obscure them at all, they just end up in the asar file as Electron-build will but the "files" within the .asar.

I can see them in there when I open the asar up however I'm struggling to get a reference to them within the code to load them into .addCommand(); as it is always coming back undefined. Hence I attempted to use the Webpack file loader to do the importing, which it does however addCommand does not like it in that format.

@BenjaminMichael
Copy link

okay so what you want is to make 2 changes to you package.json file

you want the electron build to "ignore" your script folder:

"build": {
    "productName": "example",
    "win": {
      "files":["!scripts/*"]
      "target": "NSIS",
      "icon": "myIcon.icon"
  },

see: https://www.electron.build/configuration/contents#files

also you want your build script to copy your ps scripts to your distribution folder:

  "scripts": {
    "postinstall": "install-app-deps",
    "start": "electron .",
    "package-win": "electron-packager . --overwrite --asar=true --platform=win32 --arch=x64 --icon=build/myIcon.ico --prune=true --out=release-builds --version-string.CompanyName='coolguyInc' --version-string.FileDescription='myThing' --version-string.ProductName='thingyapp';xcopy ./scripts/ ./release-builds/thingyapp-win32-x64/"
  },

Your mileage may vary with using xcopy of course but Im not sure of a platform agnostic way to write that

@BenjaminMichael
Copy link

BenjaminMichael commented Apr 19, 2018

Its probably best to copy your scripts with a post install gulp file

//gulpfile.js
var gulp = require('gulp');

gulp.task('copyscripts', function() {
    gulp.src('scripts/**.*')
    .pipe(gulp.dest('release-builds/projectName/scripts'));
 });

 gulp.task('default', ['copyscripts']);

@BenjaminMichael
Copy link

BenjaminMichael commented Apr 19, 2018

I guess file-loader is the webpack tool that gives you the equivalent of this gulp task: https://github.com/webpack-contrib/file-loader

@BenjaminMichael
Copy link

@rannn505 feel free to close this and every other issue thats open currently for node-powershell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants