modelscope·@posidron/mcp-powershell
一种模型上下文协议服务器,使AI助手能够执行PowerShell命令、检索系统信息、管理模块以及在Windows系统上运行脚本。
用于与 PowerShell 交互的 Model Context Protocol 服务器。此服务器提供了执行 PowerShell 命令、检索系统信息、管理模块等功能。
安装依赖项: bash npm install
构建项目: bash npm run build
编辑配置文件:$HOME/Library/Application Support/Claude/claude_desktop_config.json
在 mcpServers 中添加以下内容:
json
{
"mcpServers": {
"mcp-powershell": {
"command": "node",
"args": [
"/absolute/path/to/mcp-powershell/dist/index.js"
]
}
}
}
编辑配置文件:$HOME/Library/Application Support/Code/User/settings.json
在设置中添加以下内容: json "mcp": { "servers": { "mcp-powershell": { "command": "node", "args": [ "/absolute/path/to/mcp-powershell/dist/index.js" ] } } }
编辑配置文件:$HOME/.cursor/mcp.json
在 mcpServers 中添加以下内容:
json
{
"mcpServers": {
"mcp-powershell": {
"command": "node",
"args": [
"/absolute/path/to/mcp-powershell/dist/index.js"
]
}
}
}
此 PowerShell MCP 服务器提供以下工具:
执行一个 PowerShell 命令并获取结果。
参数:
使用示例:
execute_ps(command: "Get-Process | Select-Object -First 5")
检索详细的系统信息,包括操作系统详情、处理器、内存和 PowerShell 版本。
参数:无
使用示例:
get_system_info()
列出所有已安装的 PowerShell 模块及其详细信息,如名称、版本和类型。
参数:无
使用示例:
list_modules()
获取特定 PowerShell 命令的详细帮助,包括语法、参数和示例。
参数:
使用示例:
get_command_help(command: "Get-Process")
按名称或模式搜索 PowerShell 命令。
参数:
使用示例:
find_commands(search: "Process")
运行一个 PowerShell 脚本文件,并可选地传递参数。
参数:
使用示例:
run_script(scriptPath: "/path/to/script.ps1", parameters: "-Name Test -Value 123")
以开发模式运行: bash npm run dev
要添加自己的 PowerShell 工具:
src/index.tsregisterTools() 方法中添加新工具npm run build 进行构建typescript
// 在 registerTools() 方法中:
this.server.tool(
"my_ps_tool",
{
param1: z.string().describe("参数 1 的描述"),
param2: z.number().optional().describe("可选的数值参数"),
},
async ({ param1, param2 }) => {
try {
// 你的 PowerShell 命令
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: `my_ps_tool 错误: ${stderr}`,
},
],
};
}
return {
content: [
{
type: "text" as const,
text: stdout,
},
],
};
} catch (error) {
return {
isError: true,
content: [
{
type: "text" as const,
text: `my_ps_tool 错误: ${(error as Error).message}`,
},
],
};
}
} );## 安全注意事项
PowerShell 执行策略限制
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser 允许本地脚本执行找不到路径错误
找不到命令错误
Install-Module ModuleName 安装所需的模块MIT