0

我正在努力寻找使用 node-powershell 调用 powershell 脚本的正确语法,并使用 zeit/pkg 语法将所有这些编译成单个 .exe 文件。如何指定使用https://github.com/zeit/pkg中的信息编译的正确 PS1 文件的路径并使其正确启动?我的文件夹结构和我的 package.json 如下。我认为我正确地遵循了指示,但是我花了几个小时尝试一切无济于事。因此,如果我遗漏了一些明显的东西,请寻找其他眼睛。

我在项目根目录中的文件夹结构有一个资产目录(见下文)。我有一个 PS1 文件,然后是其他三个目录,这些目录具有包含 PS1 模块、信息文件等的各种文件的子目录,我的 PS1 脚本使用这些文件来运行和锁定 IE。如果使用 Electron 之类的东西打包所有内容并公开源代码,所有脚本都可以正常工作。但是我们需要锁定源代码,所以 Electron 不是一个选项。

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

我从根目录编译.exe。我知道目录中的文件在那里,因为在我添加它们之后,我在 .exe 上的文件大小增加了很多。所以它确实添加了一些东西。我似乎无法正确引用内部路径。

pkg . -t node8-win

我在我的 index.js 中运行下面的代码但是它失败说它找不到 ps1 文件

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

我得到的错误是这样的:

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

我的 package.json 内容如下:

{
  "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"
    ]
  }
}

我在下面包含的一些 --debug 标志的输出以确保我看到的是正在添加文件。我只是没有正确引用它们吗?任何帮助表示赞赏。

> [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
4

1 回答 1

0

我只是想在这里发布我的答案,因为我在试图找到答案时偶然发现了这个问题(而且只有这个问题)。我在 node-powershell 的 Github 上发布了一个类似的问题,最后自己回答了:https ://github.com/rannn505/node-powershell/issues/106#issuecomment-577249713

这是我的解释:

基本上,据我了解,您只能引用已通过使用 .exe 打包成 exe 的文件fs。任何其他方法,在这种情况下只是告诉 Powershell 打开路径,都不起作用,因为只有 Nodefs理解快照文件系统。这意味着您必须fs首先获得脚本的字符串版本,然后使用脚本块调用它。您还需要添加最后一个换行符,否则命令将不会执行并且 shell 会挂起。我也在这个例子中添加了一个参数。

...
var arg1 = 14;
var startStr = fs.readFileSync(path.join(__dirname, 'start.ps1')).toString();
ps.addCommand('& {' + startStr + '} ' + arg1 + '\n');
...
于 2020-01-22T16:31:19.667 回答