plugins/languages/powershell/skills/modules/SKILL.md
PowerShell module authoring and distribution: .psd1 manifest, .psm1 root module, function/cmdlet/variable/alias export rules, scope (Script/Private/Global), PSResourceGet (Install-PSResource) replacing the legacy PowerShellGet v2, PSGallery publish workflow, semantic versioning, module layout (Public/Private folders + dot-source loader), binary modules basics, RequiredModules and CompatiblePSEditions. Use proactively when the user asks to "写 PowerShell 模块 / ps 模块发布 / Publish-PSResource / psgallery / module manifest". Also triggers on "psd1", "psm1", "Export-ModuleMember", "PSResourceGet", "Install-PSResource", "PowerShellGet", "PSGallery".
npx skillsauth add lazygophers/ccplugin powershell-modulesInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
| 类型 | 扩展 | 用途 |
|------|------|------|
| 脚本模块 | .psm1 | 纯 PowerShell 函数,最常见 |
| 清单模块 | .psd1 | 元数据 + 指向 .psm1 / .dll,发布必备 |
| 二进制模块 | .dll | C# 编译的 cmdlet |
| manifest 模块 | .psd1 only | 聚合多个模块 |
MyModule/
├── MyModule.psd1 # 清单(版本/作者/导出列表)
├── MyModule.psm1 # 根模块,仅做加载
├── Public/ # 公开函数(自动导出)
│ ├── Get-Foo.ps1
│ └── Set-Foo.ps1
├── Private/ # 内部函数(不导出)
│ └── Get-Internal.ps1
├── Classes/
│ └── MyType.ps1
├── Tests/
│ └── MyModule.Tests.ps1 # Pester
├── en-US/
│ └── MyModule-help.xml # MAML 帮助(可选)
├── README.md
└── LICENSE
.psm1(dot-source loader)#Requires -Version 7.4
Set-StrictMode -Version 3.0
$publicFunctions = @(Get-ChildItem -Path "$PSScriptRoot/Public" -Filter '*.ps1' -ErrorAction SilentlyContinue)
$privateFunctions = @(Get-ChildItem -Path "$PSScriptRoot/Private" -Filter '*.ps1' -ErrorAction SilentlyContinue)
foreach ($file in @($privateFunctions) + @($publicFunctions)) {
try {
. $file.FullName
} catch {
Write-Error "Failed to import $($file.FullName): $_"
}
}
Export-ModuleMember -Function $publicFunctions.BaseName
.psd1@{
RootModule = 'MyModule.psm1'
ModuleVersion = '1.2.0'
GUID = 'a1b2c3d4-...' # New-Guid 生成一次
Author = 'Your Name'
CompanyName = 'Acme'
Copyright = '(c) 2026 Acme. All rights reserved.'
Description = 'One-line description'
PowerShellVersion = '7.4'
CompatiblePSEditions = @('Core') # Desktop = WinPS 5.1
RequiredModules = @(
@{ ModuleName = 'Microsoft.PowerShell.SecretManagement'; ModuleVersion = '1.1.2' }
)
FunctionsToExport = @('Get-Foo', 'Set-Foo') # 显式列出,不要 '*'
CmdletsToExport = @()
VariablesToExport = @()
AliasesToExport = @()
PrivateData = @{
PSData = @{
Tags = @('automation', 'devops')
LicenseUri = 'https://example.com/license'
ProjectUri = 'https://github.com/owner/repo'
ReleaseNotes = 'See CHANGELOG.md'
}
}
}
关键准则:
FunctionsToExport 显式枚举,禁 '*'(影响自动发现性能 + 安全)。PowerShellVersion 与 CompatiblePSEditions 必填,CI 据此选 runner。RequiredModules 写最小版本约束,让 Install-PSResource 自动拉。| 作用域 | 语法 | 含义 |
|--------|------|------|
| $script:var | 模块内全局 | .psm1 内函数共享 |
| $local:var | 当前作用域 | 函数内默认 |
| $private:var | 仅当前作用域 | 子作用域不可见 |
| $global:var | session 全局 | 慎用,污染调用者 |
| $using:var | -Parallel / Invoke-Command | 跨边界引入 |
模块函数默认无法直接读写调用者的变量;需要时显式
param()传入。
PowerShellGet v2 已废弃;PSResourceGet 提供更快、独立的资源管理。
# 安装 PSResourceGet(Win 11 / pwsh 7.4 已内置)
Install-Module Microsoft.PowerShell.PSResourceGet -Scope CurrentUser
# 配置仓库
Register-PSResourceRepository -Name PSGallery -Uri https://www.powershellgallery.com/api/v3 -Trusted
Get-PSResourceRepository
# 安装 / 卸载
Install-PSResource -Name Pester -Version '5.5.0' -Scope CurrentUser
Update-PSResource -Name Pester
Uninstall-PSResource -Name Pester
# 搜索
Find-PSResource -Name 'PSScriptAnalyzer'
# 1. 在 https://www.powershellgallery.com/account/apikeys 创建 API key
$env:NUGET_API_KEY = '...' # 或用 SecretManagement
# 2. 本地校验清单
Test-ModuleManifest -Path ./MyModule.psd1
# 3. 静态分析
Invoke-ScriptAnalyzer -Path . -Recurse -Severity Warning
# 4. 跑测试
Invoke-Pester ./Tests -Output Detailed
# 5. 发布
Publish-PSResource -Path ./MyModule -ApiKey $env:NUGET_API_KEY -Repository PSGallery
CI 模板(GitHub Actions):
- uses: PowerShell/PSResourceGet@v1
- shell: pwsh
run: |
Test-ModuleManifest ./MyModule.psd1
Invoke-ScriptAnalyzer -Path . -Recurse -Severity Warning -EnableExit
Invoke-Pester -CI
if ($env:GITHUB_REF -like 'refs/tags/v*') {
Publish-PSResource -Path ./MyModule -ApiKey $env:PSGALLERY_KEY
}
ModuleVersion 走 SemVer MAJOR.MINOR.PATCH。Prerelease 字段(PrivateData.PSData.Prerelease = 'beta1')→ 显示 1.2.0-beta1。Update-ModuleManifest -ModuleVersion ...。Import-Module ./MyModule.psd1 -Force -Verbose
Get-Module MyModule | Remove-Module
Get-Command -Module MyModule
-Force强制重载,开发期常用。生产代码禁用-Force隐藏的副作用。
[Cmdlet(VerbsCommon.Get, "Greeting")]
public class GetGreetingCommand : PSCmdlet {
[Parameter(Mandatory = true)]
public string Name { get; set; } = "";
protected override void ProcessRecord() {
WriteObject($"Hello, {Name}!");
}
}
.csproj 引用 Microsoft.PowerShell.SDK,build 产物 .dll 在 .psd1 的 RootModule 引用。
.psd1 通过 Test-ModuleManifestFunctionsToExport 显式列出CompatiblePSEditions 与 PowerShellVersion 与目标匹配tools
UI/UX 与布局设计——做界面布局/结构/导航/组件/交互的设计决策。触发:做UI/UX/布局/排版/导航/组件/交互/栅格/响应式/图表选型/字体配对。按媒介路由 HTML/Web、原生 App(iOS/Android/桌面)、CLI、TUI。需后端动态系统不适用;配色/主题/色板走姊妹 skill design-color。
tools
主题与配色设计——做颜色搭配/调色板/主题/品牌色阶/暗模式的设计决策。触发:选配色/调色/主题/色板/品牌色/暗模式/对比度/色盲/UI风格。按媒介路由 HTML/Web(CSS变量)、原生App(平台token)、CLI(ANSI)、TUI(真彩/256/16降级)。保证可访问性(对比度/色盲安全)。需后端动态系统不适用;UI/UX 布局/组件/交互走姊妹 skill design-uiux。
tools
跨任意组件(plugin/skill/agent/command)的验证驱动优化循环纪律 skill。当用户要优化某个已有组件却无明确方向、或要防止改了反而更差(自评乐观偏差 / 多维同改归因失效 / 为凑分加废话膨胀)、或要把一套通用「评分→单变量改→改后验证严格更好才留否则回滚→触顶停」的纪律套到任意组件上时使用。管优化过程本身的纪律(validation gate / ratchet / 独立验证 / 触顶停),不评单组件深度(交 skill-dev),不查插件接线(交 plugin-dev)。仅手动 /optimize-any 触发。
data-ai
两层规则记忆 (基于 .skein/spec)。planning 时 recall 召回相关规则、task finish 后 sediment 沉淀学习 + prune 自动精简过期/重复/断链规则。core 常驻硬规 + recall 按需召回, 经判定门自动写盘 (不逐次问用户)。产出 .skein/spec 下 core/recall 规则文件 + index。另支持空仓 bootstrap 播种规则基线、记忆大面积失效 (大重构/换栈) 时 reconstruct 可逆归档后按项目类型分型重建、maintain 手动体检 (超预算/stale/断链/重复/废弃, --apply 自动修复)、auto-fix (Stop hook 写 .pending-fix 标记 → main 派 skein-specer bg 跑 maintain --apply 全自动修, 断链只报告)。