| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- import type {ExecFileOptions, ExecFileSyncOptions} from 'node:child_process';
- export type ExecutePowerShellOptions = ExecFileOptions & {
- /**
- Path to PowerShell executable.
- @default powerShellPath()
- */
- readonly powerShellPath?: string;
- };
- export type ExecutePowerShellSyncOptions = ExecFileSyncOptions & {
- /**
- Path to PowerShell executable.
- @default powerShellPath()
- */
- readonly powerShellPath?: string;
- };
- export type ExecutePowerShellResult = {
- readonly stdout: string;
- readonly stderr: string;
- };
- /**
- Get the PowerShell executable path on Windows.
- @returns The path to the PowerShell executable.
- @example
- ```
- import {powerShellPath} from 'powershell-utils';
- const psPath = powerShellPath();
- //=> 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe'
- ```
- */
- export function powerShellPath(): string;
- /**
- Check if PowerShell is accessible on Windows.
- This checks if the PowerShell executable exists and has execute permissions. Useful for detecting restricted environments where PowerShell may be disabled by administrators.
- @returns A promise that resolves to true if PowerShell is accessible, false otherwise.
- @example
- ```
- import {canAccessPowerShell} from 'powershell-utils';
- if (await canAccessPowerShell()) {
- console.log('PowerShell is available');
- } else {
- console.log('PowerShell is not accessible');
- }
- ```
- */
- export function canAccessPowerShell(): Promise<boolean>;
- /**
- Execute a PowerShell command.
- @param command - The PowerShell command to execute.
- @returns A promise that resolves to the command output.
- @example
- ```
- import {executePowerShell} from 'powershell-utils';
- const {stdout} = await executePowerShell('Get-Process');
- console.log(stdout);
- ```
- */
- export function executePowerShell(
- command: string,
- options?: ExecutePowerShellOptions
- ): Promise<ExecutePowerShellResult>;
- export namespace executePowerShell {
- /**
- Standard PowerShell arguments that prefix the encoded command: `['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-EncodedCommand']`
- Exposed for debugging or for advanced use cases where you need to customize the arguments. For most cases, use `createArguments()` instead.
- */
- export const argumentsPrefix: readonly string[];
- /**
- Encode a PowerShell command as Base64 UTF-16LE.
- This encoding prevents shell escaping issues and ensures complex commands with special characters are executed reliably.
- @param command - The PowerShell command to encode.
- @returns Base64-encoded command.
- @example
- ```
- import {executePowerShell} from 'powershell-utils';
- const encoded = executePowerShell.encodeCommand('Get-Process');
- ```
- */
- export function encodeCommand(command: string): string;
- /**
- Escape a string argument for use in PowerShell single-quoted strings.
- @param value - The value to escape.
- @returns Escaped and quoted string ready for PowerShell.
- @example
- ```
- import {executePowerShell} from 'powershell-utils';
- const escaped = executePowerShell.escapeArgument("it's a test");
- //=> "'it''s a test'"
- // Use in command building
- const command = `Start-Process ${executePowerShell.escapeArgument(appName)}`;
- ```
- */
- export function escapeArgument(value: unknown): string;
- /**
- Create the full arguments array for PowerShell execution.
- Combines `argumentsPrefix` with the encoded command. Useful when using `spawn()`, `execFile()`, or other process execution methods.
- @param command - The PowerShell command.
- @returns Array of arguments ready to pass to a process spawner.
- @example
- ```
- import {spawn} from 'node:child_process';
- import {powerShellPath, executePowerShell} from 'powershell-utils';
- const args = executePowerShell.createArguments('Get-Process');
- spawn(powerShellPath(), args);
- ```
- */
- export function createArguments(command: string): string[];
- }
- /**
- Execute a PowerShell command synchronously.
- @param command - The PowerShell command to execute.
- @returns The stdout output as a string.
- @example
- ```
- import {executePowerShellSync} from 'powershell-utils';
- const stdout = executePowerShellSync('Get-Process');
- console.log(stdout);
- ```
- */
- export function executePowerShellSync(
- command: string,
- options?: ExecutePowerShellSyncOptions
- ): string;
|