run_script
Execute PowerShell scripts on Windows systems through the PowerShell MCP Server by specifying the script path and optional parameters for automation and system management tasks.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| parameters | No | Optional parameters to pass to the script | |
| scriptPath | Yes | Path to the PowerShell script file |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"parameters": {
"description": "Optional parameters to pass to the script",
"type": "string"
},
"scriptPath": {
"description": "Path to the PowerShell script file",
"type": "string"
}
},
"required": [
"scriptPath"
],
"type": "object"
}
Implementation Reference
- src/index.ts:319-357 (handler)The asynchronous handler function for the 'run_script' tool. It constructs a PowerShell command to execute the script file with optional parameters using execAsync, handles stdout/stderr, and returns formatted content or error response.async ({ scriptPath, parameters }: { scriptPath: string; parameters?: string }) => { try { const fullCommand = parameters ? `powershell -File "${scriptPath}" ${parameters}` : `powershell -File "${scriptPath}"`; const { stdout, stderr } = await execAsync(fullCommand); if (stderr) { return { isError: true, content: [ { type: 'text' as const, text: `Error running script: ${stderr}`, }, ], }; } return { content: [ { type: 'text' as const, text: stdout || 'Script executed successfully with no output.', }, ], }; } catch (error) { return { isError: true, content: [ { type: 'text' as const, text: `Error running script: ${(error as Error).message}`, }, ], }; }
- src/index.ts:315-317 (schema)Zod input schema defining 'scriptPath' as required string (path to PS script) and 'parameters' as optional string.{ scriptPath: z.string().describe('Path to the PowerShell script file'), parameters: z.string().optional().describe('Optional parameters to pass to the script'),
- src/index.ts:313-359 (registration)Registration of the 'run_script' tool on the MCP server using this.server.tool(), specifying the name, input schema, and handler function.this.server.tool( 'run_script', { scriptPath: z.string().describe('Path to the PowerShell script file'), parameters: z.string().optional().describe('Optional parameters to pass to the script'), }, async ({ scriptPath, parameters }: { scriptPath: string; parameters?: string }) => { try { const fullCommand = parameters ? `powershell -File "${scriptPath}" ${parameters}` : `powershell -File "${scriptPath}"`; const { stdout, stderr } = await execAsync(fullCommand); if (stderr) { return { isError: true, content: [ { type: 'text' as const, text: `Error running script: ${stderr}`, }, ], }; } return { content: [ { type: 'text' as const, text: stdout || 'Script executed successfully with no output.', }, ], }; } catch (error) { return { isError: true, content: [ { type: 'text' as const, text: `Error running script: ${(error as Error).message}`, }, ], }; } } );