rannn505 / child-shell

Node.js bindings 🔗 for shell
http://rannn505.github.io/child-shell/
MIT License
301 stars 71 forks source link

New-NetFireWallRule : Access is denied. #101

Open servappio opened 4 years ago

servappio commented 4 years ago

I'm trying to run the below code but I get the error New-NetFireWallRule : Access is denied.

let ps = new pshell({
      verbose: true,
      executionPolicy: "Bypass"
    });
    ps.addCommand(
      'Remove-NetFirewallRule -DisplayName "electron.exe"'
    );
    ps.addCommand(
      'New-NetFirewallRule -DisplayName "electron.exe" -Enabled True -Direction Inbound -LocalPort ANY -Protocol ANY -Profile Private,Public -Action Allow -Program  "' +
        process.execPath +
        '"'
    );
    ps.invoke()
      .then(output => {
        /**
         * successfully remove firewall rules, now create new rules
         */
console.log(output)
        ps.dispose();
      })
      .catch(err => {
        /**
         * Failed to remove firewall rule, go ahead and create new rules anyway
         */
console.log(err);
        ps.dispose();
      });
  });

Running the commands straight in PS works fine. So I put it down to permission problems but I'm a bit stuck on how to elevate PS using node-powershell. I've tried addingcommand: 'Start-Process -FilePath "powershell" -Verb RunAs' when the new instance is created and it runs without error but does not delete or create the firewall rules.

Any help would be appreciated.

rannn505 commented 4 years ago

Run with elevation