(
file: string,
args: string[] = [],
options: any = {}
)
| 77 | * powershellExecScript('script.ps1', ['arg1', 'arg2']).then(output => console.log(output)); |
| 78 | */ |
| 79 | export function powershellExecScript( |
| 80 | file: string, |
| 81 | args: string[] = [], |
| 82 | options: any = {} |
| 83 | ): ForkPromise<string> { |
| 84 | return new ForkPromise(async (resolve, reject) => { |
| 85 | try { |
| 86 | const escapedFile = escapePowershellArg(file) |
| 87 | let cmd: string |
| 88 | |
| 89 | if (args.length > 0) { |
| 90 | const escapedArgs = args.map(escapePowershellArg).join(' ') |
| 91 | cmd = `powershell.exe -NoProfile -WindowStyle Hidden -ExecutionPolicy Bypass -Command "Unblock-File -LiteralPath ${escapedFile}; & ${escapedFile} ${escapedArgs}"` |
| 92 | } else { |
| 93 | cmd = `powershell.exe -NoProfile -WindowStyle Hidden -ExecutionPolicy Bypass -Command "Unblock-File -LiteralPath ${escapedFile}; & ${escapedFile}"` |
| 94 | } |
| 95 | |
| 96 | const result: any = await (execPromise as any)(cmd, options) |
| 97 | resolve(result.stdout?.toString() || '') |
| 98 | } catch (error) { |
| 99 | reject(error) |
| 100 | } |
| 101 | }) |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * This function executes a file with PowerShell, allowing for arguments and options. |
nothing calls this directly
no test coverage detected