Skip to main content
Glama

PowerShell MCP 服务器

用于与 PowerShell 交互的模型上下文协议服务器。此服务器提供用于执行 PowerShell 命令、检索系统信息、管理模块等的工具。

要求

  • Node.js 18+

  • PowerShell 5.1 或 PowerShell Core 7+

Related MCP server: MCP Personal Assistant Agent

安装

  1. 安装依赖项:

    npm install
  2. 构建项目:

    npm run build

配置

对于克劳德桌面

编辑配置: $HOME/Library/Application\ Support/Claude/claude_desktop_config.json

添加到 mcpServers:

{ "mcpServers": { "mcp-powershell": { "command": "node", "args": [ "/absolute/path/to/mcp-powershell/dist/index.js" ] } } }

对于 VS Code

编辑配置: $HOME/Library/Application\ Support/Code/User/settings.json

添加到设置:

"mcp": { "servers": { "mcp-powershell": { "command": "node", "args": [ "/absolute/path/to/mcp-powershell/dist/index.js" ] } } }

对于 Cursor IDE

编辑配置: $HOME/.cursor/mcp.json

添加到 mcpServers:

{ "mcpServers": { "mcp-powershell": { "command": "node", "args": [ "/absolute/path/to/mcp-powershell/dist/index.js" ] } } }

可用工具

此 PowerShell MCP 服务器提供以下工具:

执行_ps

执行 PowerShell 命令并获取结果。

Parameters: - command (string): PowerShell command to execute

使用示例:

execute_ps(command: "Get-Process | Select-Object -First 5")

获取系统信息

检索详细的系统信息,包括操作系统详细信息、处理器、内存和 PowerShell 版本。

Parameters: None

使用示例:

get_system_info()

列表模块

列出所有已安装的 PowerShell 模块,包括名称、版本和类型等详细信息。

Parameters: None

使用示例:

list_modules()

获取命令帮助

获取特定 PowerShell 命令的详细帮助,包括语法、参数和示例。

Parameters: - command (string): PowerShell command to get help for

使用示例:

get_command_help(command: "Get-Process")

查找命令

按名称或模式搜索 PowerShell 命令。

Parameters: - search (string): Search term for PowerShell commands

使用示例:

find_commands(search: "Process")

运行脚本

运行带有可选参数的 PowerShell 脚本文件。

Parameters: - scriptPath (string): Path to the PowerShell script file - parameters (string, optional): Optional parameters to pass to the script

使用示例:

run_script(scriptPath: "/path/to/script.ps1", parameters: "-Name 'Test' -Value 123")

发展

要在开发模式下运行:

npm run dev

扩展服务器

要添加您自己的 PowerShell 工具:

  1. 编辑src/index.ts

  2. registerTools()方法中添加新工具

  3. 遵循现有的模式以实现一致的错误处理

  4. 使用npm run build进行构建

添加工具示例

// In the registerTools() method: this.server.tool( "my_ps_tool", { param1: z.string().describe("Description of parameter 1"), param2: z.number().optional().describe("Optional numeric parameter"), }, async ({ param1, param2 }) => { try { // Your PowerShell command const command = `Your-PowerShell-Command -Param1 "${param1}" ${param2 ? `-Param2 ${param2}` : ''}`; const { stdout, stderr } = await execAsync(`powershell -Command "${command.replace(/"/g, '\\"')}"`); if (stderr) { return { isError: true, content: [ { type: "text" as const, text: `Error in my_ps_tool: ${stderr}`, }, ], }; } return { content: [ { type: "text" as const, text: stdout, }, ], }; } catch (error) { return { isError: true, content: [ { type: "text" as const, text: `Error in my_ps_tool: ${(error as Error).message}`, }, ], }; } } );

安全注意事项

  • 该服务器直接在您的系统上执行 PowerShell 命令

  • 命令以与运行 MCP 服务器的进程相同的权限执行

  • 暴露破坏性操作时要小心

  • 考虑对敏感命令实施额外验证

故障排除

常见问题

  1. PowerShell 执行策略限制

    • 您可能需要调整 PowerShell 执行策略以允许脚本执行

    • 使用Set-ExecutionPolicy RemoteSigned -Scope CurrentUser允许本地脚本

  2. 未找到路径错误

    • 确保文件路径是绝对路径或相对于工作目录正确路径

    • 为您的操作系统使用适当的路径分隔符

  3. 未找到命令错误

    • 某些命令可能需要安装特定模块

    • 使用Install-Module ModuleName安装所需的模块

执照

麻省理工学院

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/posidron/mcp-powershell'

If you have feedback or need assistance with the MCP directory API, please join our Discord server