0

I am struggling to find the right syntax of calling a powershell script using node-powershell and compile all that into a single .exe file using the zeit/pkg syntax. How can I specify the path to the correct PS1 file that is compiled in using the information from https://github.com/zeit/pkg and have it fire off correctly? My folder structure and my package.json is below. I think I am following the directions correctly however I have spent a few hours trying things to no avail. So asking for other eyes in case I am missing something blatantly obvious.

My folder structure in the root of the project has an assets directory (see below). In that I have a single PS1 file, and then three other directories that have subdirectories of various files that contain PS1 modules, info files, etc. that my PS1 script uses to run and lock down IE. All the scripts work correctly if run using something like Electron to package everything up and expose the source. However we need to lock the source code down so Electron is not an option.

assets
  IE-Start.ps1 <-- this is a file
  InternetExplorer <-- this is a folder with files I read in with my script
  modules <-- this is a folder with files I read in with my script
  Windows <-- this is a folder with files I read in with my script
index.js
package-lock.json
package.json
README.md
node_modules

I compile the .exe as this from the root. I know the files in the directories are in there because after I added them my file size on the .exe went up a LOT. So it did add something. I just cannot seem to reference the internal paths correctly.

pkg . -t node8-win

I am running the code below in my index.js however it fails saying it cannot find the ps1 file

let scriptPath = path.join(__dirname, 'assets/IE-Start.ps1')
ps.addCommand(scriptPath);

The errors I get are along the lines of this:

Cannot find path 'C:\snapshot\ielockdown\assets\InternetExplorer\' 
because it does not exist.

My package.json contents are below:

{
  "name": "ielockdown",
  "version": "1.0.0",
  "description": "IE Lockdown Tool compiled for Win10",
  "main": "index.js",
  "scripts": {
    "start": "npm start"
  },
  "author": "Dale Bingham",
  "license": "ISC",
  "dependencies": {
    "node-powershell": "^4.0.0",
    "path": "^0.12.7"
  },
  "bin": "index.js",
  "pkg": {
    "scripts": "*.js",
    "assets": [
      "assets/*.ps1",
      "assets/**/*"
    ],
    "targets" : [
      "node8"
    ]
  }
}

Some of the output of the --debug flag I included below to ensure what I am seeing is that the files are being added. I am just not referencing them correctly maybe? Any help is appreciated.

> [debug] The file was included as asset content
  /Users/dalebingham/cingulara/ielockdown/assets/IE-Start.ps1
> [debug] The file was included as asset content
  /Users/dalebingham/cingulara/ielockdown/assets/modules/Excel.psm1
> [debug] The file was included as asset content
  /Users/dalebingham/cingulara/ielockdown/assets/modules/GitHub.psm1
> [debug] The file was included as asset content
  /Users/dalebingham/cingulara/ielockdown/assets/modules/GroupPolicy.psm1
Dale Bingham
  • 362
  • 4
  • 13

1 Answers1

0

I just wanted to post my answer to this here, because I stumbled upon this question (and ONLY this question) when trying to find an answer. I posted a similar question on node-powershell's Github, and ended up answering it myself just now: https://github.com/rannn505/node-powershell/issues/106#issuecomment-577249713

Here's my explanation:

Basically, from what I understand, you can only reference files that have been packaged into an exe by using fs. Any other method, in this case simply telling Powershell to open the path, doesn't work because only Node's fs understands the snapshot filesystem. That means you will have to go through fs first to get a string version of your script, then call it using a script block. You also need to add a final line break, because otherwise the command won't execute and the shell hangs. I've added an argument to this example as well.

...
var arg1 = 14;
var startStr = fs.readFileSync(path.join(__dirname, 'start.ps1')).toString();
ps.addCommand('& {' + startStr + '} ' + arg1 + '\n');
...
MKarrow
  • 41
  • 4