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

Relative paths to PS scripts on Windows machines #45

Closed
AdamCankaya opened this issue Feb 13, 2018 · 17 comments
Closed

Relative paths to PS scripts on Windows machines #45

AdamCankaya opened this issue Feb 13, 2018 · 17 comments
Labels
Question A relevant question

Comments

@AdamCankaya
Copy link

I'm having some issues loading a ps1 file using a relative path on a Windows machine.

When I try to add a ps1 using a direct path, such as this, everything works great:
ps.addCommand("C:/dev/powershell-electron-demo/Get-Drives", [{ ComputerName: computer }]);

But then when I try a relative path, such as this, I get an error about it not being found:
ps.addCommand("./Get-Drives", [{ ComputerName: computer }]);

I'm 100% positive my Get-Drives.ps1 is in my root folder, the same folder with my index.html and renderer.js. Everything works great with a direct path. I've tried every combination of forward slash, back slash, double slash, single periods, double periods, etc. Any ideas?

@BenjaminMichael
Copy link

Is your script saved in the .ps1 format? If yes can you try this more basic syntax:

 ps.addCommand(`./Get-Drives.ps1 -ComputerName ${computerName}`);

and if that doesnt work can you please share your version of node, npm, node-powershell, and Windows PowerShell?

@AdamCankaya
Copy link
Author

Yes, my script is saved as Get-Drives.ps1

I tried your command and no luck. Also tried it with single quotes and double quotes.

I am using node 8.9.4, npm 5.6.0, node-powershell 3.3.0, electron 1.8.2-beta.4 and Windows 7 SP1 64bit.

Could I possibly need to add the .ps1 file to my package.json or something? Or format the file location string into a path object for PS?

I saw an earlier issue was resolved by using Shell.executionStringBuilder, but I have not been able to find out anything about this. Think its relevant?

@BenjaminMichael
Copy link

BenjaminMichael commented Feb 14, 2018

Go to Start > Run > powershell.exe
type in:

$PSVersionTable.PSVersion

what does it say?

If powershell.exe is not found maybe you need to install Windows Management Framework (go ahead and use 5.0) or maybe you're using PowerShell Core? There is an issue about pwsh not found and I have a PR open.

@AdamCankaya
Copy link
Author

AdamCankaya commented Feb 14, 2018

Here is the output for the PSVersion:

Major  Minor  Build  Revision
-----  -----  -----  --------
3     0       -1      -1

I am brand new to Powershell so I will need to do some research on the versions and on the Windows Management Framework.

@BenjaminMichael
Copy link

Is the error about node-powershell not found? Are you importing node-powershell into your render.js? Could you try the CJS syntax:

const powershell = require('node-powershell');

Even better can you share your render.js in its entirety or put the project on github?

@AdamCankaya
Copy link
Author

Hello, apologies for the delayed response.

I have put my whole example project (called AWMT-electron) onto GitHub.

https://github.com/AdamCankaya/AWMT-electron

It is a simple webpage with a text link to a JS function that launches a PS script called releaseIP that just calls "ipconfig /release".

On renderer.js, lines 11 and 12, you will see my issue. If I use the command on line 11, a direct path to the releaseIP.ps1 file, everything works fine. However, if I comment out line 11, and then run line 12, which tries to call "./releaseIP" then it fails. It gives an error about releaseIP not being found.

@BenjaminMichael
Copy link

BenjaminMichael commented Feb 28, 2018

  1. You need to add your node_modules folder to a .gitignore file so the modules do not get copied to github
  2. Your package.json for a node project should have been initialized and should have dependencies listed. Installing your modules with the -S flag automagically does this.
//for example:
{
  "name": "awm-app",
  "version": "1.0.0",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "dependencies": {
    "electron": "^1.8.2",
    "jquery": "^3.3.1",
    "node-powershell": "^3.3.1"
  }
}

3.Note how this package.json also uses symantic versioning (1.0 is invalid 1.0.0 is valid)
4.Note how this package.json includes a start script so I can start electron

All that stuff aside, this worked for me first try with the command on line 12 not commented-out.

To recap:
in git bash I did:

git clone https://github.com/AdamCankaya/AWMT-electron

then

 cd AWMT-electron/AWMT-app/
npm install jquery -S
npm install node-powershell -S
npm install electron -S

in visual studio code (or any text editor) I changed line 3 of package.json:

"version": "1.0.0",

and then also in a text editor in package.json I added to line 5:

"scripts": {
    "start": "electron ."
  },

and then back to my command line

npm start

and it worked the first try. I had to ipconfig /renew just to make this comment. I have no idea why it wouldn't work for you.

I am on Powershell 5 and Windows 10 but I doubt thats the issue

@AdamCankaya
Copy link
Author

Thanks for the tips about my project setup. I will make the changes and try to duplicate your way of starting everything up. I'll report back soon.

@SethStalley
Copy link

SethStalley commented May 9, 2018

Just wanted to report that I am having the same problem with relative paths on Windows. I too am using this within an electron project. Absolute paths work fine.

Node: v8.8.3
electron: v2.0.0
Powershell: v5.1.1
Windows 10

Update:
Apologies. My mistake, it is working fine.
For anyone with this issue, remember that the path is relative to the execution point and not your present JS file.

@dhuber666
Copy link

I have the same problem.
I have following folder structure:

https://github.com/dhuber666/Powershell-SAS-Tool
Look at the src/components/scriptsComponents Folder. Here is the .js file that starts the .ps1 file located in src/PowershellScripts

What is the correct path to start this script AddMailboxPermissions.ps1 from the js file src/components/scriptsComponents/AddMailboxPermissions.js ?

I tried several paths but I always get the error that it couldn't find the ps1 file.

@BenjaminMichael
Copy link

I see you are using Electron and Webpack. Are you sure your issue is the same as this guy's? (His issue seemed to be setting up a basic project) or was your issue more like #49 where you make your electron build and powershell.exe cannot see the script you packed up in your asar? Does it work in dev mode before you do your build?

If this is your issue like I told that guy you will want to copy your powershell scripts over to your production build with a post install script.

@dhuber666
Copy link

dhuber666 commented Jun 29, 2018

Hi,

nope it just works when I give it the direct path like it is in the script right now:
const scriptPath = require("path").resolve('src/PowershellScripts', './AddMailboxPermissions.ps1')

But this ofc give me the full path starting at my C: etc. I need the project path. So when I build it and I put it on a network drive (so my colleagues can use it) the ps1 scripts still get loaded.

I read through the issue but I don't know why I have to copy the ps1 scripts with a gulp task.
The ps1 scripts are also in my build folder C:\Users\Huberdo\Projekte\Powershell-SAS-App\Client\builds\basic-electron-react-boilerplate-win32-x64\resources\app\src\PowershellScripts

But then the path isn't correct anymore. So I need a way that it's using the relative path. I'm not so familiar with node's path and path's in general.

I think I just need to find the right path function to get this task done. So it's always something like that:

../../PowershellScripts/AddMailboxPermissions.ps1

But then again I don't know what path's the node-powershell accepts. Because if I put the ps1 in the same directory as the js file it gets loaded from and do something like that:

ps.addCommand('./AddMailboxPermissions.ps1', commands); it still doesn't find it.

And thank you so much for always trying to help me @BenjaminMichael 👍

@BenjaminMichael
Copy link

The paths are relative to the execution point not the Javascript file. You would need to specify the path from the point of execution:

ps.addCommand('./path/to/my/files/AddMailboxPermissions.ps1')

even if the javascript is in that same folder.

Note that I cannot tell your point of execution as you have no typical electron startup script in your package.json.

Also note that powershell.exe cannot see inside an asar so for a production electron build of your electron app you would need a way to provide the program with the scripts. This is a different issue from executing the script in dev mode where your project files are not compressed into an asar.
If you have more questions please specify what scripts from your package.json you are running when this doesn't work for you because different scripts will have different answers.

@dhuber666
Copy link

dhuber666 commented Jun 29, 2018

I have this kind of setup because I used a boilerplate from github that includes electron and react with webserver.

It's so depressing. I have the finished app working when I run it with npm run dev but when I build it, it's not working.

The closest I have gotten so far is using this:

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

This is working fine in the dev environment. But when I build it and run it I get the following error in the app:

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

But the path should be C:\Users\Huberdo\Projekte\Powershell-SAS-App\Client\builds\basic-electron-react-boilerplate-win32-x64\resources\app\src\PowershellScripts\GetRemoteMac.ps1

So I'm so confused right now why it resolves this path in the error message and not the correct one. If it would It would be working.

I don't run any scripts from package.json besides the npm run dev, npm run build and npm run package

@BenjaminMichael
Copy link

Webpack and React needlessly complicate such a project in my opinion. I usually start off with https://github.com/electron/electron-quick-start

That said, your problem is universal to electron. The electron build process does more than just copy all your files and folders over. It also compresses it into a structure that windows cannot easily see inside of. For that reason you need a post build script that creates a folder to hold the scripts and copies the scripts from your original source into this new folder.

You can see in my example project's package.json I have 2 scripts:

 "package-win": "electron-packager . --overwrite --asar=true --platform=win32 --arch=x64 --icon=build/adgw.ico --prune=true --out=release-builds --version-string.CompanyName='ben' --version-string.FileDescription='ADGroupWrangler' --version-string.ProductName='adgroupwrangler'",
    "post-package-win": "gulp"

First I: node package-win
then I: node post-package-win

The second script copies over my powershell with a gulp task.

@dhuber666
Copy link

Thanks again.
I will take a closer look again tomorrow and see what I can do about it. Where should I copy the powershell scripts then and which path should I take?

@rannn505
Copy link
Owner

Closed due to lack of response

@rannn505 rannn505 added the Question A relevant question label Dec 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question A relevant question
Projects
None yet
Development

No branches or pull requests

5 participants