How to run a powershell script in an angular app?

I have converted my angular project to an electron one using this tutorial and now I want to run a powershell script in my app.component.ts. How would I achieve this? I tried using exec and child_process but that would not work.

a.ps1:

$a = Get-ExecutionPolicy
write-host $a

app.component.ts:

import { Component, OnInit } from '@angular/core';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
    title="basic-app";

    constructor() {}

    ngOnInit() {this.onPress()}

    onPress() {
        const { exec } = require('child_process');

        const scriptPath="C:/Users/conra/Desktop/a.ps1";

        exec(
            `powershell.exe -ExecutionPolicy Bypass -File "${scriptPath}"`,
            (error, stdout, stderr) => {
                console.log(stdout)
            }
        );
    }
}

I expected to be able to use require but I got the following error:

Error: src/app/app.component.ts:16:26 - error TS2591: Cannot find name 'require'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.

16         const { exec } = require('child_process');

I did the instructions and there was no change in output. Thanks!

Leave a Comment