用途
该技能为开发者提供生产级 PowerShell 脚本、工具和 GUI 开发的完整知识参考,围绕微软官方最佳实践和 PowerShell 生态展开,可用于编写脚本、设计函数、开发 Windows 图形界面、集成 PowerShell Gallery 模块以及进行 Windows 自动化。
主要能力
脚本与函数开发
技能给出了标准脚本结构(#Requires 版本声明、注释帮助块、[CmdletBinding()]、param 块及 begin/process/end 块)和函数模板,强调以下约定:
- 使用 Get-Verb 认可的动词,采用 Verb-Noun 命名格式
- 强类型参数并配合验证属性(如 ValidateNotNullOrEmpty)
- 通过 ValueFromPipeline / ValueFromPipelineByPropertyName 支持管道输入
- 对破坏性操作提供 -WhatIf / -Confirm(SupportsShouldProcess)
- 使用 -PassThru 控制输出
错误处理方面,技能展示了 try/catch 按异常类型捕获、Write-Error 返回、必要时重新抛出等写法;可读性方面推荐 splatting 传参;管道方面建议及时流式输出并正确处理 process 块中的逐项处理。
GUI 开发
简单对话框推荐 Windows Forms(加载 System.Windows.Forms 与 System.Drawing 程序集,创建 Form 并设置标题、尺寸、居中位置等属性);复杂界面推荐 WPF/XAML。技能引用 gui-development.md 提供控件、事件与模板的进一步说明。
PowerShell Gallery 集成
使用 PSResourceGet 搜索与安装模块,示例包括:
- Find-PSResource -Name 'ModuleName' -Repository PSGallery
- Install-PSResource -Name 'ModuleName' -Scope CurrentUser -TrustRepository
并提供 scripts/Search-Gallery.ps1 增强搜索(在旧版环境可回退到 Find-Module)。
模块推荐与文档核实
技能列出常见模块类别作为起点(Azure 的 Az 系列、测试的 Pester/PSScriptAnalyzer、控制台的 PSReadLine/Terminal-Icons、密钥管理的 Microsoft.PowerShell.SecretManagement、Web 的 Pode/PoshRSJob、GUI 的 WPFBot3000/PSGUI),但要求推荐前必须核实。
文档核实强调抓取原始 Markdown 而非渲染页面,给出了几类 URL 模板:
- 核心 PowerShell:https://raw.githubusercontent.com/MicrosoftDocs/PowerShell-Docs/main/reference/{version}/{Module}/{Cmdlet}.md
- 概念主题:https://raw.githubusercontent.com/MicrosoftDocs/PowerShell-Docs/main/reference/{version}/Microsoft.PowerShell.Core/About/{about_Topic}.md
- 仅 Windows:https://raw.githubusercontent.com/MicrosoftDocs/windows-powershell-docs/main/docset/winserver2025-ps/{Module}/{Cmdlet}.md
- Gallery / 打包:https://raw.githubusercontent.com/MicrosoftDocs/powershell-docs-psget/main/powershell-gallery/powershellget-3.x/Microsoft.PowerShell.PSResourceGet/{Cmdlet}.md
- 卫星模块:https://raw.githubusercontent.com/MicrosoftDocs/PowerShell-Docs-Modules/main/reference/ps-modules/{Module}/{Cmdlet}.md
版本取值可为 5.1、7.4、7.5、7.6、7.7;无明确信号时建议同时获取 7.5 和 5.1 并说明差异。URL 构造有三条规则:路径大小写敏感、不能指向文件夹(以 / 结尾会 404)、将 github.com 的 blob 链接转换为 raw.githubusercontent.com。
失败时的回退链依次为:原始 Markdown → GitHub contents API(https://api.github.com/repos/{owner}/{repo}/contents/{path})→ learn.microsoft.com → WebSearch({Cmdlet-Name} site:learn.microsoft.com/en-us/powershell)→ 本地执行 Get-Help / Get-Command。全部失败时应明确说明无法核实,而非猜测。
模块核实则直接抓取 https://www.powershellgallery.com/packages/{ModuleName},提取模块名、最新版本、最后更新日期、总下载量及是否弃用或 unlisted;404 时用 WebSearch 确认,或在工具不可用时运行 ${CLAUDE_PLUGIN_ROOT}/skills/powershell-knowledge/scripts/Search-Gallery.ps1 -Name 'ModuleName'。
参考文件与脚本
- references/doc-sources.md:文档路由、原始 URL 映射、404 排查
- references/best-practices.md:命名、参数、管道、错误处理、代码风格
- references/gui-development.md:Windows Forms、WPF、控件、事件、模板
- references/powershellget.md:模块查找、安装、更新、发布
- scripts/Search-Gallery.ps1:格式化 Gallery 搜索,带旧版回退
使用方式
该技能以 Claude Code 插件技能形式提供,安装与启用方式暂时未提供。使用时由 AI 在 PowerShell 相关开发任务中参照上述规则执行,并优先通过 WebFetch 抓取原始 Markdown 与 Gallery 页面核实信息。
适用场景
- 编写符合微软规范的 PowerShell 脚本与高级函数
- 构建 Windows Forms / WPF 图形工具
- 搜索、安装和推荐 PowerShell Gallery 模块
- 核实 cmdlet 语法、参数与版本差异
- Windows 系统与服务器自动化
限制
- 技能中的模块清单只是常见起点,必须实时核实,不能直接当作事实引用
- 文档核实依赖 WebFetch/WebSearch、GitHub API(未认证每小时 60 次)等外部工具
- 无网络工具时需用户本地运行 Get-Help / Get-Command
- 未提供具体的安装命令、收费模式或平台支持说明