1

I want to execute a powershell script on the start of the electron App. I tried to add the following into the <head> in my index.html :

<head>
<script type="text/javascript">
    var spawn = require("child_process").spawn;
    spawn("powershell.exe",[".\myPSScript.ps1"]);       
</script>
</head>
<body>
...
</body>

This did not work. When adding:

document.write("Hello")

it shows up in the app so I can be sure that the <script>is being run. When I run the script itself from powershell-terminal it runs with no issues and does what it should.

I also tried giving the absolute path in:

spawn("powershell.exe",["C:\folder\folder2\myPSScript.ps1"]);       

The script deletes all .db files in a folder on the machine.

Get-ChildItem -Path 'MY_Wonderfull_Path' *.db | Where-Object { $_.CreationTime -lt (get-date 2021-09-09) } | foreach { Remove-Item -Path $_.FullName }

I have no idea why it is not running. Is electron not able to spawn a new child process? Is it an issue with permissions?

3
  • 1
    I believe you need to double all backslashes in the absolute path: spawn("powershell.exe",["C:\\folder\\folder2\\myPSScript.ps1"]); Commented Sep 10, 2021 at 12:02
  • THis is right. Please make it an answer so I can approve it :) Commented Sep 10, 2021 at 12:17
  • You're right, I have now posted as answer so the question can be marked 'done'. Commented Sep 10, 2021 at 12:50

1 Answer 1

4

As requested, here my comment as answer.

You need to double the backslashes in the path to escape them.

spawn("powershell.exe",["C:\\folder\\folder2\\myPSScript.ps1"]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.