import * as Shell from './src/shell'; import * as Command from './src/command'; import { createLogger } from './src/utils/logger'; import { monitorCommandOutput } from './src/interactive'; import { CommandParams, InteractiveCommandParams, InteractivePromptOutput, WinRMParams, } from './src/types'; import { detectAuthMethod } from './src/utils/auth'; export { Shell, Command, monitorCommandOutput }; /** * Create WinRM connection parameters with auto-detected authentication. */ function createWinRMParams( host: string, port: number, username: string, password: string, useHttps?: boolean, rejectUnauthorized?: boolean ): WinRMParams { return { host, port, path: '/wsman', username, password, authMethod: detectAuthMethod(username), useHttps, rejectUnauthorized, }; } /** * Execute a command on a remote Windows machine via WinRM * @param command - Command to execute * @param host - Target host address * @param username - Username for authentication (supports local, DOMAIN\user, or user@domain.com formats) * @param password - Password for authentication * @param port - WinRM port (typically 5985 for HTTP, 5986 for HTTPS) * @param usePowershell - Whether to use PowerShell (default: false) * @param useHttps - Use HTTPS instead of HTTP (default: false) * @param rejectUnauthorized - Reject self-signed certificates (default: true) * @returns Command output */ export async function runCommand( command: string, host: string, username: string, password: string, port: number, usePowershell = false, useHttps = false, rejectUnauthorized = true ): Promise { const logger = createLogger('runCommand'); const params = createWinRMParams( host, port, username, password, useHttps, rejectUnauthorized ); logger.debug('Using auth method', { authMethod: params.authMethod }); let shellParams: CommandParams | null = null; try { const shellId = await Shell.doCreateShell(params); logger.debug('shellId', shellId); shellParams = { ...params, shellId }; const commandParams = { ...shellParams, command }; const commandId = usePowershell ? await Command.doExecutePowershell(commandParams) : await Command.doExecuteCommand(commandParams); logger.debug('commandId', commandId); const receiveParams = { ...commandParams, commandId }; const output = await Command.doReceiveOutput(receiveParams); logger.debug('output', output); return output; } finally { if (shellParams) { await Shell.doDeleteShell(shellParams); } } } /** * Execute a PowerShell command on a remote Windows machine via WinRM * @param command - PowerShell command to execute * @param host - Target host address * @param username - Username for authentication * @param password - Password for authentication * @param port - WinRM port (typically 5985 for HTTP, 5986 for HTTPS) * @param useHttps - Use HTTPS instead of HTTP (default: false) * @param rejectUnauthorized - Reject self-signed certificates (default: true) * @returns Command output */ export async function runPowershell( command: string, host: string, username: string, password: string, port: number, useHttps = false, rejectUnauthorized = true ): Promise { return runCommand( command, host, username, password, port, true, useHttps, rejectUnauthorized ); } /** * Execute an interactive command that responds to prompts via WinRM * @param command - Command to execute * @param host - Target host address * @param username - Username for authentication (supports local, DOMAIN\user, or user@domain.com formats) * @param password - Password for authentication * @param port - WinRM port (typically 5985 for HTTP, 5986 for HTTPS) * @param prompts - Array of prompt patterns and responses * @param executionTimeout - Overall command timeout in ms (default: 60000) * @param httpTimeout - HTTP request timeout in ms * @param pollInterval - Output polling interval in ms (default: 500) * @param useHttps - Use HTTPS instead of HTTP (default: false) * @param rejectUnauthorized - Reject self-signed certificates (default: true) * @returns Command output */ export async function runInteractiveCommand( command: string, host: string, username: string, password: string, port: number, prompts: InteractivePromptOutput[], executionTimeout?: number, httpTimeout?: number, pollInterval?: number, useHttps = false, rejectUnauthorized = true ): Promise { const logger = createLogger('runInteractiveCommand'); const params = createWinRMParams( host, port, username, password, useHttps, rejectUnauthorized ); logger.debug('Using auth method', { authMethod: params.authMethod }); let shellParams: CommandParams | null = null; try { const shellId = await Shell.doCreateShell(params); logger.debug('shellId', shellId); shellParams = { ...params, shellId }; const commandParams: CommandParams = { ...shellParams, command, httpTimeout, }; const commandId = await Command.doExecuteCommand(commandParams); logger.debug('commandId', commandId); const interactiveParams: InteractiveCommandParams = { ...commandParams, commandId, prompts, executionTimeout, pollInterval, }; const output = await monitorCommandOutput(interactiveParams); logger.debug('output', output); return output; } finally { if (shellParams) { await Shell.doDeleteShell(shellParams); } } } /** * Execute an interactive PowerShell command that responds to prompts via WinRM * @param command - PowerShell command to execute * @param host - Target host address * @param username - Username for authentication (supports local, DOMAIN\user, or user@domain.com formats) * @param password - Password for authentication * @param port - WinRM port (typically 5985 for HTTP, 5986 for HTTPS) * @param prompts - Array of prompt patterns and responses * @param executionTimeout - Overall command timeout in ms (default: 60000) * @param httpTimeout - HTTP request timeout in ms * @param pollInterval - Output polling interval in ms (default: 500) * @param useHttps - Use HTTPS instead of HTTP (default: false) * @param rejectUnauthorized - Reject self-signed certificates (default: true) * @returns Command output */ export async function runInteractivePowershell( command: string, host: string, username: string, password: string, port: number, prompts: InteractivePromptOutput[], executionTimeout?: number, httpTimeout?: number, pollInterval?: number, useHttps = false, rejectUnauthorized = true ): Promise { const logger = createLogger('runInteractivePowershell'); const params = createWinRMParams( host, port, username, password, useHttps, rejectUnauthorized ); logger.debug('Using auth method', { authMethod: params.authMethod }); let shellParams: CommandParams | null = null; try { const shellId = await Shell.doCreateShell(params); logger.debug('shellId', shellId); shellParams = { ...params, shellId }; const commandParams: CommandParams = { ...shellParams, command, httpTimeout, }; const commandId = await Command.doExecutePowershell(commandParams, true); logger.debug('commandId', commandId); const interactiveParams: InteractiveCommandParams = { ...commandParams, commandId, prompts, executionTimeout, pollInterval, }; const output = await monitorCommandOutput(interactiveParams); logger.debug('output', output); return output; } finally { if (shellParams) { await Shell.doDeleteShell(shellParams); } } }