PowerShell模块开发入门指南

模块基础结构

1
2
3
4
# 创建模块目录结构
New-Item -Path MyModule\Public -ItemType Directory
New-Item -Path MyModule\Private -ItemType Directory
New-Item -Path MyModule\MyModule.psd1 -ItemType File

清单文件配置

1
2
3
4
5
6
@{
ModuleVersion = '1.0'
RootModule = 'MyModule.psm1'
FunctionsToExport = @('Get-SystemInfo')
RequiredModules = @('PSScriptAnalyzer')
}

函数导出实践

1
2
3
4
5
6
7
8
9
10
11
function Get-SystemInfo {
[CmdletBinding()]
param()

$os = Get-CimInstance Win32_OperatingSystem
[PSCustomObject]@{
OSName = $os.Caption
Version = $os.Version
InstallDate = $os.InstallDate
}
}

模块发布流程

1
2
3
4
5
6
7
8
9
# 生成模块清单
New-ModuleManifest -Path .\MyModule\MyModule.psd1 \
-Author 'YourName' \
-Description '系统信息获取模块'

# 发布到本地仓库
Publish-Module -Path .\MyModule \
-Repository LocalRepo \
-NuGetApiKey 'AzureDevOps'