UNPKG

winrm-client

Version:
209 lines (208 loc) 8 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.monitorCommandOutput = exports.Command = exports.Shell = void 0; exports.runCommand = runCommand; exports.runPowershell = runPowershell; exports.runInteractiveCommand = runInteractiveCommand; exports.runInteractivePowershell = runInteractivePowershell; const Shell = __importStar(require("./src/shell")); exports.Shell = Shell; const Command = __importStar(require("./src/command")); exports.Command = Command; const logger_1 = require("./src/utils/logger"); const interactive_1 = require("./src/interactive"); Object.defineProperty(exports, "monitorCommandOutput", { enumerable: true, get: function () { return interactive_1.monitorCommandOutput; } }); /** * 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 * @param password - Password for authentication * @param port - WinRM port (typically 5985 for HTTP) * @param usePowershell - Whether to use PowerShell (default: false) * @returns Command output */ async function runCommand(command, host, username, password, port, usePowershell = false) { const logger = (0, logger_1.createLogger)('runCommand'); const auth = 'Basic ' + Buffer.from(username + ':' + password, 'utf8').toString('base64'); const params = { host, port, path: '/wsman', auth: auth, }; let shellParams = null; try { const shellId = await Shell.doCreateShell(params); logger.debug('shellId', shellId); shellParams = { ...params, shellId }; const commandParams = { ...shellParams, command }; let commandId; if (usePowershell) { commandId = await Command.doExecutePowershell(commandParams); } else { commandId = 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) * @returns Command output */ async function runPowershell(command, host, username, password, port) { return runCommand(command, host, username, password, port, true); } /** * 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 * @param password - Password for authentication * @param port - WinRM port (typically 5985 for HTTP) * @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) * @returns Command output */ async function runInteractiveCommand(command, host, username, password, port, prompts, executionTimeout, httpTimeout, pollInterval) { const logger = (0, logger_1.createLogger)('runInteractiveCommand'); let shellParams = null; try { const auth = 'Basic ' + Buffer.from(username + ':' + password, 'utf8').toString('base64'); const params = { host, port, path: '/wsman', auth: auth, }; const shellId = await Shell.doCreateShell(params); logger.debug('shellId', shellId); shellParams = { ...params, shellId }; const commandParams = { ...shellParams, command, httpTimeout, }; const commandId = await Command.doExecuteCommand(commandParams); logger.debug('commandId', commandId); const interactiveParams = { ...commandParams, commandId, prompts, executionTimeout, pollInterval, }; const output = await (0, interactive_1.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 * @param password - Password for authentication * @param port - WinRM port (typically 5985 for HTTP) * @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) * @returns Command output */ async function runInteractivePowershell(command, host, username, password, port, prompts, executionTimeout, httpTimeout, pollInterval) { let shellParams = null; try { const logger = (0, logger_1.createLogger)('runInteractivePowershell'); const auth = 'Basic ' + Buffer.from(username + ':' + password, 'utf8').toString('base64'); const params = { host, port, path: '/wsman', auth: auth, }; const shellId = await Shell.doCreateShell(params); logger.debug('shellId', shellId); shellParams = { ...params, shellId }; const commandParams = { ...shellParams, command, httpTimeout, }; const commandId = await Command.doExecutePowershell(commandParams, true); logger.debug('commandId', commandId); const interactiveParams = { ...commandParams, commandId, prompts, executionTimeout, pollInterval, }; const output = await (0, interactive_1.monitorCommandOutput)(interactiveParams); logger.debug('output', output); return output; } finally { if (shellParams) { await Shell.doDeleteShell(shellParams); } } }