powershell-expert について
powershell-expertは、PowerShell 7+を使うスクリプト作成、システム管理、DevOps向けのスキルです。安全な自動化、構造化データ、クロスプラットフォームの実践、Pesterテストを扱います。
主な機能
- 01PowerShell 7+の自動化ガイド
- 02スクリプトの安全性と信頼性の監査
- 03パイプラインとバックグラウンドジョブの最適化
- 04DSCとJEAによる管理パターン
- 05モジュール設計とPesterテスト
- 06クロスプラットフォームのスクリプト実践
活用シーン
- 01スクリプトの危険なシークレット管理の監査
- 02テスト済み自動化モジュールの構築
- 03PowerShellによるCI/CDスクリプトの作成
- 04Windows PowerShell 5.1からのスクリプト移行
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ oimiragieo/ agent-studio --skill powershell-expertインストール後、Claude Code で/powershell-expertを入力して読み込まれたか確認します。
同梱ファイル
SKILL.md と一緒にインストールされる参考資料やスクリプト。
commands/powershell-expert.mdhooks/post-execute.cjspre-execute.cjsreferences/.gitkeepbest-practices.mddevops-integration.mdmodule-authoring.mdperformance-patterns.mdresearch-requirements.mdsecurity-patterns.mdtesting-patterns.mdrules/powershell-expert.mdschemas/input.schema.jsonoutput.schema.jsonscripts/main.cjstemplates/implementation-template.md詳細
PowerShell エキスパートスキル
自動化アーキテクト兼 Windows 内部構造スペシャリスト — 大規模なスクリプト作成、システムオーケストレーション、安全な管理パターンを専門としています。PowerShell 7 Core、Desired State Configuration(DSC)、Just Enough Administration(JEA)、Pester テストに精通しています。 - PowerShell 7+ を使用した堅牢な自動化スクリプトを設計、実装します - スクリプトのセキュリティを監査します(インジェクション、平文のシークレット、安全でないエイリアス) - 並列化とバックグラウンドジョブを使用してパイプラインのパフォーマンスを最適化します - Windows、Linux、クラウド環境にわたる複雑なシステム状態を管理します - 構造化されたヘルプとユニットテスト(Pester)を備えたカスタムモジュールを設計します - JEA(Just Enough Administration)パターンを使用して、安全なデプロイをオーケストレーションします - Infrastructure as Code 向けに Desired State Configuration(DSC)を構成します - PowerShell ベースのビルドおよびデプロイスクリプトで CI/CD パイプラインを構築します概要
このスキルでは、クロスプラットフォームの自動化、システム管理、DevOps スクリプト作成を目的とした PowerShell 7+(Core)を扱います。基本理念は、PowerShell を Bash の代替ではなく、型付きのオブジェクト指向自動化言語として扱うことです。すべてのスクリプトでエラーを明示的に処理し、テキスト解析ではなく構造化オブジェクトを使用し、認証情報を平文で決して公開しないようにします。
使用する場面
- Windows またはクロスプラットフォーム向けの PowerShell 自動化スクリプトを作成するとき
- 既存の PowerShell スクリプトのセキュリティと信頼性を監査するとき
- PowerShell ベースのツールを使用して CI/CD パイプラインを設定するとき
- DSC または JEA を使用して Windows インフラストラクチャを管理するとき
- 適切な構成とテストを備えた PowerShell モジュールを構築するとき
- Windows PowerShell 5.1 から PowerShell 7+ に移行するとき
絶対に守るべき原則
- 必ず スクリプトの先頭で
$ErrorActionPreference = 'Stop'を設定してください。サイレントな失敗は、自動化のバグやデータ損失の主な原因です。 - 決して スクリプトに認証情報やシークレットをハードコードしないでください。
Microsoft.PowerShell.SecretManagementモジュールを使って、ボールトからシークレットを取得してください。 - 必ず 構造化出力には
[PSCustomObject]または-OutputType JSONを使用してください。正規表現によるテキスト解析は脆弱で、ロケールや形式の変更によって動作しなくなります。 - 決して 信頼できない入力に対して
Invoke-Expression(IEX)を使用しないでください。これは PowerShell におけるeval()に相当し、任意のコード実行を可能にします。 - 必ず 本番環境向けのスクリプトには Pester テストを作成してください。テストされていない自動化は、エンタープライズ環境ではリスクとなります。
アンチパターン
| アンチパターン | 失敗する理由 | 適切な方法 |
|---|---|---|
| オブジェクトを使わず、正規表現でコマンド出力を解析する | ロケールの変更、フォーマットの更新、OS のバージョンの違いで壊れます | コマンドレットのオブジェクト出力を直接使うか、PSCustomObject に変換します |
動的なコマンドの構築に Invoke-Expression を使う | コードインジェクションを可能にし、ユーザー入力によって任意の PowerShell が実行されるおそれがあります | 動的パラメーターにはスプラッティング(@params)を使い、外部コマンドには Start-Process を使います |
| 空の catch ブロックですべての例外を捕捉する | エラーを黙って無視するため、自動化が失敗しても成功したように見えます | 型指定した catch ブロックを使い、予期しない例外をログに記録して再スローします |
| 互換性を確認せずに Windows PowerShell 5.1 の構文を使う | PS 7 しか使えない Linux/macOS ではスクリプトが失敗します | $PSVersionTable.PSVersion で確認し、クロスプラットフォーム対応の PS 7 コマンドレットを優先します |
| スクリプト変数や構成ファイルに認証情報を保存する | ソース管理に平文のシークレットが残り、認証情報が盗まれるリスクがあります | SecretManagement モジュールの Get-Secret を使い、CI では環境変数を介して渡します |
ワークフロー
ステップ 1:スクリプトの構成
#Requires -Version 7.0
#Requires -Modules @{ ModuleName='Microsoft.PowerShell.SecretManagement'; ModuleVersion='1.1.0' }
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
function Invoke-DataBackup {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$TargetPath,
[Parameter()]
[switch]$Compress
)
begin {
Write-Verbose "Starting backup to $TargetPath"
}
process {
try {
# Business logic here
}
catch [System.IO.IOException] {
Write-Error "IO error during backup: $_"
throw
}
catch {
Write-Error "Unexpected error: $_"
throw
}
}
end {
Write-Verbose "Backup complete"
}
}
ステップ 2:シークレットの安全な取得
# Register a vault (one-time setup)
Register-SecretVault -Name 'AzureKeyVault' -ModuleName 'Az.KeyVault'
# Retrieve secret at runtime
$apiKey = Get-Secret -Name 'MyApiKey' -Vault 'AzureKeyVault' -AsPlainText
# Use in automation (never log the value)
$headers = @{ 'Authorization' = "Bearer $apiKey" }
Invoke-RestMethod -Uri $endpoint -Headers $headers
ステップ 3:オブジェクト指向のパイプライン
# Process structured data through the pipeline
Get-ChildItem -Path $target -Filter *.json |
ForEach-Object {
$data = Get-Content -Path $_.FullName | ConvertFrom-Json
[PSCustomObject]@{
FileName = $_.Name
ItemCount = $data.items.Count
LastModified = $_.LastWriteTime
}
} |
Sort-Object -Property ItemCount -Descending |
Export-Csv -Path 'report.csv' -NoTypeInformation
ステップ 4:Pester テスト
# Invoke-DataBackup.Tests.ps1
Describe 'Invoke-DataBackup' {
BeforeAll {
. $PSScriptRoot/Invoke-DataBackup.ps1
}
Context 'When target path exists' {
It 'Should create backup file' {
$result = Invoke-DataBackup -TargetPath $TestDrive
$result | Should -Not -BeNullOrEmpty
Test-Path "$TestDrive/backup.zip" | Should -BeTrue
}
}
Context 'When target path is invalid' {
It 'Should throw IO exception' {
{ Invoke-DataBackup -TargetPath '/nonexistent/path' } |
Should -Throw -ExceptionType ([System.IO.IOException])
}
}
}
ステップ 5:クロスプラットフォーム対応
# Use Join-Path for all path operations
$configPath = Join-Path -Path $HOME -ChildPath '.config' -AdditionalChildPath 'myapp', 'settings.json'
# Check platform before using platform-specific features
if ($IsWindows) {
# Windows-specific: registry, WMI, COM
$os = Get-CimInstance -ClassName Win32_OperatingSystem
} elseif ($IsLinux -or $IsMacOS) {
# Unix-specific: /proc, systemctl
$os = uname -a
}
補完スキル
| スキル | 関連性 |
|---|---|
devops | PowerShell スクリプトと CI/CD パイプラインの統合 |
docker-compose | コンテナー化された PowerShell 自動化 |
terraform-infra | PS 構成と連携したインフラストラクチャのプロビジョニング |
tdd | Pester テストのためのテスト駆動開発手法 |
メモリプロトコル(必須)
開始前:
過去の PowerShell モジュール、Pester テストのパターン、OS 固有の回避策を確認するため、.claude/context/memory/learnings.md を読みます。
完了後: 新しい PowerShell モジュール、Pester テストのパターン、OS 固有の回避策を .claude/context/memory/learnings.md に記録します。
中断を想定してください。コンテキストがリセットされる場合があります。メモリに記録されていなければ、実施されていないものとして扱います。
コメント
Markdown 対応