PowerShell 技能连载 - Windows 包管理自动化

适用于 PowerShell 5.1 及以上版本(Windows)

在 Windows 上管理软件安装是运维工作中最繁琐的环节之一——每台服务器需要安装几十个工具和运行时,手动安装耗时且容易遗漏版本不一致。Windows Package Manager(winget)和 Chocolatey 的出现改变了这一局面,它们让 Windows 也拥有了类似 Linux apt-get / yum 的包管理体验。结合 PowerShell,可以实现软件的批量安装、版本锁定和自动更新。

本文将讲解 winget 和 Chocolatey 的 PowerShell 集成,以及如何构建标准化的软件清单。

阅读更多

PowerShell 技能连载 - PowerShell Gallery 与模块管理

适用于 PowerShell 5.1 及以上版本

PowerShell 的强大很大程度上归功于其丰富的模块生态。PowerShell Gallery 是微软官方的模块仓库,目前托管了超过 4000 个模块,涵盖 Azure 管理、AWS 集成、安全审计、数据库操作等几乎所有领域。掌握模块的查找、安装、发布和版本管理,是每个 PowerShell 用户提升效率的必经之路。

本文将讲解 PowerShell Gallery 的使用技巧、模块的安装与更新策略、私有仓库搭建,以及如何发布自己的模块。

阅读更多

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'
PowerShell 技术 QQ 群