はじめに
PowerShell DSC を使ってなんらかの構成をする際に、標準リソースだけでは対処できない場合があります。
そのような時は、例えば下記の様な便利なリソースが公開されていますので、一度 目的が達成できるかチェックすると良いかもしれません。
DSC Resource Kit (All Modules)
しかし、それでも事足りない状況は当然発生しますので、そんな時はリソースを自作しちゃえばいいだけですね!
でも、どうやってリソースを作るのかサッパリわからず、下記の TechNet を参考にしました。
Build Custom Windows PowerShell Desired State Configuration Resources
ということで、今回は自分用のメモとしてカスタムリソースの作り方を記録しておきます。
xDSCResourceDesigner
とりあず、他のDSCリソースの中身をみてみましょう。
下図は DSC Resource Kit でダウンロードできる xWebAdministration の MSFT_xWebAppPool.psm1 の内容です。
![201501291132]()
こういうファイルを1から手動で作っていくのは億劫なのでやってられません。
なので、このようなファイルなどなどを簡単に作成してくれるモジュールがあります。
xDSCResourceDesigner
このモジュールを読み込んで、必要なパラメーターを与えるだけでモジュールを作る上で必要になるファイルなどなどを作成してくれます。
では、早速作っていきます。
ダウンロードした xDSCResourceDesigner を下記のフォルダに配置してモジュールを読み込めるようにします。
$env:ProgramFiles\WindowsPowerShell\Modules
Import-Module でモジュールを読み込み、Get-Command で読み込んだモジュールが利用可能かどうかを確認します。
Import-Module xDSCResourceDesigner
Get-Command -Module xDSCResourceDesigner
![201501291134]()
つづいて、カスタムリソースで利用するプロパティを定義して .psm1 と .mof ファイルを作成します。
今回のカスタムリソースはあくまで参考程度に作るものなので、指定したパスに該当するファイルが存在している のような簡単なお話でリソースを書いていきたいと思います。
$FilePath = New-xDscResourceProperty -Name FilePath -Type String -Attribute Key
$Ensure = New-xDscResourceProperty -Name Ensure -Type String -Attribute Write -ValidateSet "Present","Absent"
New-xDscResource `
-Name "SampleDSCresource" `
-Property $FilePath, $Ensure `
-Path "C:\Program Files\WindowsPowerShell\Modules\SampleDSCmodule" `
-ClassVersion 1.0 `
-FriendlyName "SampleDSCresource" `
-Force
![201501291135]()
これで、$env:ProgramFiles\WindowsPowerShell\Modules フォルダ内には、下図のようにファイルとフォルダが作られます。
![201501291138]()
次に .psd1 ファイルを作ります。このファイルは下記の New-ModuleManifest で作成できます。
$param = @{
guid = [Guid]::NewGuid()
author = "64"
company = "DSC Demos"
moduleversion = "1.0"
description = "Creating a sample DSC resource"
path= "C:\Program Files\WindowsPowerShell\Modules\SampleDSCmodule\SampleDSCmodule.psd1"
}
New-ModuleManifest @param
![201501291139]()
.psd1 ファイルはModulesフォルダのカスタムリソースフォルダの直下に作成します。
![201501291140]()
基本的に必要となるフォルダやファイルの作成は以上で、次にカスタムリソースで やりたいこと を書いていきます。
カスタムモジュールの機能
.psm1 ファイルをエディターで開いて、自作したい内容を Get-TargetResource Set-TargetResource Test-TargetResource それぞれのファイルに書くことになります。
Get-TargetResource
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[parameter(Mandatory = $true)]
[System.String]
$FilePath,
[parameter(Mandatory = $true)]
[ValidateSet("Present","Absent")]
[System.String]
$Ensure
)
$returnValue = @{
FilePath = $FilePath
Ensure = $Ensure
}
$returnValue
}
Set-TargetResource
function Set-TargetResource
{
[CmdletBinding()]
param
(
[parameter(Mandatory = $true)]
[System.String]
$FilePath,
[parameter(Mandatory = $true)]
[ValidateSet("Present","Absent")]
[System.String]
$Ensure
)
$IsExists = Test-Path -Path $FilePath;
if($Ensure -eq "Present")
{
if ($IsExists)
{
return
}
Out-File -FilePath $FilePath -InputObject $Content
}else{
if($IsExists)
{
Remove-Item -Path $FilePath
}
}
}
Test-TargetResource
function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[parameter(Mandatory = $true)]
[System.String]
$FilePath,
[parameter(Mandatory = $true)]
[ValidateSet("Present","Absent")]
[System.String]
$Ensure
)
$bool = $false;
$IsExists = Test-Path -Path $FilePath;
if (($Ensure -eq "Present") -and ($IsExists))
{
Write-Debug -Message ("Ensure: {0}`t IsExists: {1}" -F $Ensure, $IsExsis) -Debug
$bool = $true
}
elseif (($Ensure -eq "Present") -and (!$IsExists))
{
Write-Debug -Message ("Ensure: {0}`t IsExists: {1}" -F $Ensure, $IsExsis) -Debug
$bool = $false
}
elseif (($Ensure -eq "Absent") -and ($IsExists))
{
Write-Debug -Message ("Ensure: {0}`t IsExists: {1}" -F $Ensure, $IsExsis) -Debug
$bool = $false
}
elseif (($Ensure -eq "Absent") -and (!$IsExists))
{
Write-Debug -Message ("Ensure: {0}`t IsExists: {1}" -F $Ensure, $IsExsis) -Debug
$bool = $true
}
$bool
}
Export-ModuleMember -Function *-TargetResource
動作確認
以上で、自作リソースの作成が完了しましたので、例えば下記の様な Configuration を書くことが出来ます。
Configuration SampleExecTest
{
param (
$MachineName="localhost"
)
Import-DscResource -ModuleName SampleDSCmodule
node $MachineName
{
SampleDSCresource Sample
{
FilePath = "C:\Temp\hoge.txt"
Ensure = "Present"
}
}
}
SampleExecTest -MachineName "localhost" -OutputPath .
Start-DscConfiguration -path .\SampleExecTest -Wait -Verbose
結果はこんな感じで ちゃんと動いていますね。
![201501291151]()