Git Product home page Git Product logo

Comments (17)

BenjaminMichael avatar BenjaminMichael commented on May 27, 2024

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?

from child-shell.

AdamCankaya avatar AdamCankaya commented on May 27, 2024

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?

from child-shell.

BenjaminMichael avatar BenjaminMichael commented on May 27, 2024

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.

from child-shell.

AdamCankaya avatar AdamCankaya commented on May 27, 2024

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.

from child-shell.

BenjaminMichael avatar BenjaminMichael commented on May 27, 2024

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?

from child-shell.

AdamCankaya avatar AdamCankaya commented on May 27, 2024

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.

from child-shell.

BenjaminMichael avatar BenjaminMichael commented on May 27, 2024
  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

from child-shell.

AdamCankaya avatar AdamCankaya commented on May 27, 2024

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.

from child-shell.

SethStalley avatar SethStalley commented on May 27, 2024

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.

from child-shell.

dhuber666 avatar dhuber666 commented on May 27, 2024

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.

from child-shell.

BenjaminMichael avatar BenjaminMichael commented on May 27, 2024

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.

from child-shell.

dhuber666 avatar dhuber666 commented on May 27, 2024

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 👍

from child-shell.

BenjaminMichael avatar BenjaminMichael commented on May 27, 2024

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.

from child-shell.

dhuber666 avatar dhuber666 commented on May 27, 2024

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

from child-shell.

BenjaminMichael avatar BenjaminMichael commented on May 27, 2024

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.

from child-shell.

dhuber666 avatar dhuber666 commented on May 27, 2024

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?

from child-shell.

rannn505 avatar rannn505 commented on May 27, 2024

Closed due to lack of response

from child-shell.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.