【问题标题】:Electron executing powershell scriptElectron 执行 powershell 脚本
【发布时间】:2021-11-06 20:38:15
【问题描述】:

我想在电子应用程序启动时执行一个 powershell 脚本。我尝试在index.html<head> 中添加以下内容:

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

这不起作用。添加时:

document.write("Hello")

它显示在应用程序中,因此我可以确定 &lt;script&gt; 正在运行。当我从 powershell-terminal 运行脚本本身时,它可以正常运行并执行应有的操作。

我也尝试在以下位置给出绝对路径:

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

脚本会删除机器上某个文件夹中的所有.db 文件。

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

我不知道为什么它没有运行。电子不能产生一个新的子进程吗?是权限问题吗?

【问题讨论】:

  • 我相信您需要将绝对路径中的所有反斜杠加倍:spawn("powershell.exe",["C:\\folder\\folder2\\myPSScript.ps1"]);
  • 这是对的。请把它作为一个答案,以便我批准它:)
  • 你说得对,我现在已经发布了答案,所以问题可以标记为“完成”。

标签: javascript windows powershell electron


【解决方案1】:

根据要求,我的评论作为答案。

您需要将路径中的反斜杠加倍以将其转义。

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

【讨论】:

    猜你喜欢
    • 2019-03-14
    • 2012-09-18
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 2014-01-06
    • 2023-04-02
    • 2011-07-16
    • 2021-09-19
    相关资源
    最近更新 更多