|
| 1 | +# `runPowerShellScript` |
| 2 | + |
| 3 | +Function that executes an PowerShell script. |
| 4 | + |
| 5 | +{% hint style="info" %} |
| 6 | +Only available on Windows |
| 7 | +{% endhint %} |
| 8 | + |
| 9 | +## Signature |
| 10 | + |
| 11 | +```ts |
| 12 | +function runPowerShellScript<T>( |
| 13 | + script: string, |
| 14 | + options?: { |
| 15 | + signal?: AbortSignal; |
| 16 | + timeout?: number; |
| 17 | + parseOutput?: ParseExecOutputHandler<T>; |
| 18 | + }, |
| 19 | +): Promise<T>; |
| 20 | +``` |
| 21 | + |
| 22 | +### Arguments |
| 23 | + |
| 24 | +- `script` is the script to execute. |
| 25 | + |
| 26 | +With a few options: |
| 27 | + |
| 28 | +- `options.signal` is a Signal object that allows you to abort the request if required via an AbortController object. |
| 29 | +- `options.timeout` is a number. If greater than `0`, the parent will send the signal `SIGTERM` if the script runs longer than timeout milliseconds. By default, the execution will timeout after 10000ms (eg. 10s). |
| 30 | +- `options.parseOutput` is a function that accepts the output of the script as an argument and returns the data the hooks will return - see [ParseExecOutputHandler](#parseexecoutputhandler). By default, the function will return `stdout` as a string. |
| 31 | + |
| 32 | +### Return |
| 33 | + |
| 34 | +Returns a Promise which resolves to a string by default. You can control what it returns by passing `options.parseOutput`. |
| 35 | + |
| 36 | +## Example |
| 37 | + |
| 38 | +```tsx |
| 39 | +import { showHUD } from "@raycast/api"; |
| 40 | +import { runPowerShellScript } from "@raycast/utils"; |
| 41 | + |
| 42 | +export default async function () { |
| 43 | + const res = await runPowerShellScript( |
| 44 | + ` |
| 45 | +Write-Host "hello, world." |
| 46 | +`, |
| 47 | + ); |
| 48 | + await showHUD(res); |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +## Types |
| 53 | + |
| 54 | +### ParseExecOutputHandler |
| 55 | + |
| 56 | +A function that accepts the output of the script as an argument and returns the data the function will return. |
| 57 | + |
| 58 | +```ts |
| 59 | +export type ParseExecOutputHandler<T> = (args: { |
| 60 | + /** The output of the script on stdout. */ |
| 61 | + stdout: string; |
| 62 | + /** The output of the script on stderr. */ |
| 63 | + stderr: string; |
| 64 | + error?: Error | undefined; |
| 65 | + /** The numeric exit code of the process that was run. */ |
| 66 | + exitCode: number | null; |
| 67 | + /** The name of the signal that was used to terminate the process. For example, SIGFPE. */ |
| 68 | + signal: NodeJS.Signals | null; |
| 69 | + /** Whether the process timed out. */ |
| 70 | + timedOut: boolean; |
| 71 | + /** The command that was run, for logging purposes. */ |
| 72 | + command: string; |
| 73 | + /** The options passed to the script, for logging purposes. */ |
| 74 | + options?: ExecOptions | undefined; |
| 75 | +}) => T; |
| 76 | +``` |
0 commit comments