MCPcopy Create free account
hub / github.com/xpf0000/FlyEnv / powershellExecScript

Function powershellExecScript

src/fork/util/PowerShell.ts:79–102  ·  view source on GitHub ↗
(
  file: string,
  args: string[] = [],
  options: any = {}
)

Source from the content-addressed store, hash-verified

77 * powershellExecScript('script.ps1', ['arg1', 'arg2']).then(output => console.log(output));
78 */
79export 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.

Callers

nothing calls this directly

Calls 3

escapePowershellArgFunction · 0.85
resolveFunction · 0.85
toStringMethod · 0.45

Tested by

no test coverage detected