index.d.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import type {ExecFileOptions, ExecFileSyncOptions} from 'node:child_process';
  2. export type ExecutePowerShellOptions = ExecFileOptions & {
  3. /**
  4. Path to PowerShell executable.
  5. @default powerShellPath()
  6. */
  7. readonly powerShellPath?: string;
  8. };
  9. export type ExecutePowerShellSyncOptions = ExecFileSyncOptions & {
  10. /**
  11. Path to PowerShell executable.
  12. @default powerShellPath()
  13. */
  14. readonly powerShellPath?: string;
  15. };
  16. export type ExecutePowerShellResult = {
  17. readonly stdout: string;
  18. readonly stderr: string;
  19. };
  20. /**
  21. Get the PowerShell executable path on Windows.
  22. @returns The path to the PowerShell executable.
  23. @example
  24. ```
  25. import {powerShellPath} from 'powershell-utils';
  26. const psPath = powerShellPath();
  27. //=> 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe'
  28. ```
  29. */
  30. export function powerShellPath(): string;
  31. /**
  32. Check if PowerShell is accessible on Windows.
  33. This checks if the PowerShell executable exists and has execute permissions. Useful for detecting restricted environments where PowerShell may be disabled by administrators.
  34. @returns A promise that resolves to true if PowerShell is accessible, false otherwise.
  35. @example
  36. ```
  37. import {canAccessPowerShell} from 'powershell-utils';
  38. if (await canAccessPowerShell()) {
  39. console.log('PowerShell is available');
  40. } else {
  41. console.log('PowerShell is not accessible');
  42. }
  43. ```
  44. */
  45. export function canAccessPowerShell(): Promise<boolean>;
  46. /**
  47. Execute a PowerShell command.
  48. @param command - The PowerShell command to execute.
  49. @returns A promise that resolves to the command output.
  50. @example
  51. ```
  52. import {executePowerShell} from 'powershell-utils';
  53. const {stdout} = await executePowerShell('Get-Process');
  54. console.log(stdout);
  55. ```
  56. */
  57. export function executePowerShell(
  58. command: string,
  59. options?: ExecutePowerShellOptions
  60. ): Promise<ExecutePowerShellResult>;
  61. export namespace executePowerShell {
  62. /**
  63. Standard PowerShell arguments that prefix the encoded command: `['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-EncodedCommand']`
  64. Exposed for debugging or for advanced use cases where you need to customize the arguments. For most cases, use `createArguments()` instead.
  65. */
  66. export const argumentsPrefix: readonly string[];
  67. /**
  68. Encode a PowerShell command as Base64 UTF-16LE.
  69. This encoding prevents shell escaping issues and ensures complex commands with special characters are executed reliably.
  70. @param command - The PowerShell command to encode.
  71. @returns Base64-encoded command.
  72. @example
  73. ```
  74. import {executePowerShell} from 'powershell-utils';
  75. const encoded = executePowerShell.encodeCommand('Get-Process');
  76. ```
  77. */
  78. export function encodeCommand(command: string): string;
  79. /**
  80. Escape a string argument for use in PowerShell single-quoted strings.
  81. @param value - The value to escape.
  82. @returns Escaped and quoted string ready for PowerShell.
  83. @example
  84. ```
  85. import {executePowerShell} from 'powershell-utils';
  86. const escaped = executePowerShell.escapeArgument("it's a test");
  87. //=> "'it''s a test'"
  88. // Use in command building
  89. const command = `Start-Process ${executePowerShell.escapeArgument(appName)}`;
  90. ```
  91. */
  92. export function escapeArgument(value: unknown): string;
  93. /**
  94. Create the full arguments array for PowerShell execution.
  95. Combines `argumentsPrefix` with the encoded command. Useful when using `spawn()`, `execFile()`, or other process execution methods.
  96. @param command - The PowerShell command.
  97. @returns Array of arguments ready to pass to a process spawner.
  98. @example
  99. ```
  100. import {spawn} from 'node:child_process';
  101. import {powerShellPath, executePowerShell} from 'powershell-utils';
  102. const args = executePowerShell.createArguments('Get-Process');
  103. spawn(powerShellPath(), args);
  104. ```
  105. */
  106. export function createArguments(command: string): string[];
  107. }
  108. /**
  109. Execute a PowerShell command synchronously.
  110. @param command - The PowerShell command to execute.
  111. @returns The stdout output as a string.
  112. @example
  113. ```
  114. import {executePowerShellSync} from 'powershell-utils';
  115. const stdout = executePowerShellSync('Get-Process');
  116. console.log(stdout);
  117. ```
  118. */
  119. export function executePowerShellSync(
  120. command: string,
  121. options?: ExecutePowerShellSyncOptions
  122. ): string;