powershell_execute
Execute PowerShell commands locally to automate system administration tasks and streamline Windows management processes.
Instructions
Ejecutar comandos PowerShell localmente
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| command | Yes | Comando PowerShell a ejecutar |
Implementation Reference
- src/index.js:562-584 (handler)Handler logic for the 'powershell_execute' tool within the CallToolRequestSchema switch statement. Calls executePowerShell and formats the response.case 'powershell_execute': log('info', `⚡ Ejecutando PowerShell: ${args.command}`); const psResult = await executePowerShell(args.command, args.timeout); return { content: [ { type: 'text', text: `✅ PowerShell ejecutado:\n\n${psResult.output}` } ], isError: false, metadata: { tool: 'powershell_execute', exitCode: psResult.exitCode, executionTime: psResult.executionTime, rateLimitInfo: { remainingRequests: rateLimiter.getRemainingRequests() }, timestamp: new Date().toISOString() } };
- src/index.js:211-305 (helper)Core helper function that executes PowerShell commands locally, handles stdout/stderr, timeouts, and returns structured results.function executePowerShell(command, timeout = null) { return new Promise((resolve, reject) => { const startTime = Date.now(); const actualTimeout = timeout || parseInt(process.env.COMMAND_TIMEOUT) || DEFAULT_CONFIG.COMMAND_TIMEOUT; log('debug', 'Ejecutando comando PowerShell', { command, timeout: actualTimeout }); // Validar comando if (!command || typeof command !== 'string') { return reject(new Error('Comando PowerShell inválido')); } const psExecutable = getPowerShellExecutable(); const childProcess = spawn(psExecutable, ['-Command', command], { stdio: ['pipe', 'pipe', 'pipe'], shell: false // Mejor seguridad sin shell wrapper }); let stdout = ''; let stderr = ''; let isResolved = false; childProcess.stdout.on('data', (data) => { stdout += data.toString(); }); childProcess.stderr.on('data', (data) => { stderr += data.toString(); }); childProcess.on('close', (code) => { if (isResolved) return; isResolved = true; const executionTime = Date.now() - startTime; const result = { success: code === 0, output: stdout, error: stderr || null, exitCode: code, executionTime }; log('debug', 'Comando PowerShell completado', result); if (code === 0) { resolve(result); } else { reject(result); } }); childProcess.on('error', (error) => { if (isResolved) return; isResolved = true; const result = { success: false, output: stdout, error: error.message, exitCode: -1, executionTime: Date.now() - startTime }; log('error', 'Error ejecutando PowerShell', result); reject(result); }); // Timeout mejorado const timeoutId = setTimeout(() => { if (isResolved) return; isResolved = true; try { childProcess.kill('SIGKILL'); } catch (e) { log('warn', 'Error matando proceso', e); } const result = { success: false, output: stdout, error: `Timeout de ${actualTimeout}ms excedido`, exitCode: -1, executionTime: actualTimeout }; log('warn', 'Timeout en comando PowerShell', result); reject(result); }, actualTimeout); childProcess.on('close', () => clearTimeout(timeoutId)); childProcess.on('error', () => clearTimeout(timeoutId)); }); }
- src/index.js:432-450 (registration)Tool registration in the ListToolsRequestSchema handler, including name, description, and input schema.{ name: 'powershell_execute', description: 'Ejecutar comandos PowerShell localmente', inputSchema: { type: 'object', properties: { command: { type: 'string', description: 'Comando PowerShell a ejecutar' }, timeout: { type: 'number', description: 'Timeout en milisegundos (opcional)' } }, required: ['command'] } }, {