tech.guitarrapc.cóm

Technical updates

PowerShell DSC Advent Calendar 2014 : Day 9 Resourceの配置

これは、PowerShell DSC Advent Calendar 2014 - Adventar 9日目の記事です。

さて、9日目ったら9日目です。

今日はリソース(Resource) の配置です。標準のリソース以外の操作をしたくなったらこれですね!では見てみましょう。

目次

リソースってなんだっけ

覚えてますか?3日目でChefと比較しましたね。

対象 DSC Chef 概要
どうやるか
(ロジック)
リソース
(Resource)
クックブック
(Cookbook)
どういう状態であるべきか
何をどういう状態に
(呼出し)
コンフィグレーション
(Configuration)
レシピ
(Recipe)
ロジックを呼び出してあるべき状態を宣言型構文で指示
  • コンフィグレーションがどういう状態にするかを宣言的に記述したもの
  • リソースは、その状態に至るロジック

リソースがあれば、それに対応した状態をコンフィグレーションで記述できる => やれることが増える!です。はい。

リソースは公開されています

別にリソースは自分で書かなくてはいけないものではなく、オープンソースに公開されたものが数多くあります。 有名どころを挙げておきましょう。

オーナー 公開元 URL
Microsoft TechNetScriptCenter(Script Browser & Script Analyzer) Browse code samples | Microsoft Learn
Micorsoft GitHub PowerShell Team · GitHub
Microsoft PowerShellGet(PowerShell Gallery) PowerShell Gallery | Home
PowerShellOrg GitHub GitHub - PowerShellOrg/DSC: DSC Tools and Documentation
guitarrapc Github GitHub - guitarrapc/DSCResources: WARNING THIS REPO IS DEPRECATED!! ALL SOURCES ARE MOVED TO GraniResource.

順にみておきましょうか。

TechNetScriptCenter (Script Browser & Script Analyzer)

以前も紹介した、Microsoft の スクリプト公開スペースです。数年前からあるもので、すでに多くの PowerShell のスクリプトベースが公開されています。

gallery.technet.microsoft.com

URL からのアクセスになり、体系的にまとまっているわけでもなくとてつもなく使いにくいです。

2013年に Script Browser & Script Analyzer が公開されてからは、PowerShell ISE から検索、インストールが可能になり良くなりましたがしかしないですねぇ。

https://www.microsoft.com/ja-JP/download/details.aspx?id=42525

PowerShell Team - GitHub

2015年4月から、PowerShell Team は GitHub で OSS にコードを公開始めました。その中に PowerShell Teamが TechNetで公開していた DSC Resource も含まれています。

github.com

詳細はこっちの記事で!

tech.guitarrapc.com

GitHub に公開されたことで、 Submodule での参照などコードベースの組み込みがしやすくなりました。

しかし、一般に導入するには利用しやすいかというと、あと一歩感があります。

PowerShellOrg - GitHub

PowerShell.Org もカスタムリソースを GitHub で公開しています。OSS としてはこっちがメジャーですね。

ちょっと質に問題があって微妙なリソースも多いのですが、Issue なり Pull Request してください。

github.com

GraniResource - GitHub

私が所属する謎社で使っているリソースを公開しています。

github.com

謎社で書いたリソースが GraniResoure です。実際本番環境で安定して利用しているリソースであり、基本的にはベストプラクティスとテストを通過したものばかりなので、ぜひ利用してみていただけると。

一部 PowerShell Team のリソースで使いにくいところを修正したりしたカスタムリソースもあります。

PowerShellGet

DSC Resource は、Github、Gist で公開されたものもあります。けどちょっと探しにくかったり利用しにくいかも。

探しにくいのは利用者からすると気づけかれないというサイテーであり、利用しにくいのはハードルになるでしょう。

そこで、マイクロソフトの PowerShell Team が主導して NuGet をベースにタグで PowerShellモジュール関連を apt-getライクに取得できるようにごにょごにょしている PowerShellGetがあります。

f:id:guitarrapc_tech:20141211041233p:plain

これを使うと PowerShell v5 では NuGet ライクにモジュールがインストールできます。

検索もたったこれだけ。

Repository Version  Name                         Description                                                           
---------- -------  ----                         -----------                                                           
PSGallery  1.0.0.0  AppDomainConfig              Manipulate AppDomain configuration for your current PowerShell sess...
PSGallery  1.0      AutoBrowse                   AutoBrowse is a module that lets you automate browsing with Interne...
PSGallery  1.1.0.0  AutoVars                     Allows for the definition of automatic (calculated) variables in yo...
PSGallery  0.8      Await                        Await - A modern implementation of EXPECT for Windows. For a demo, ...
PSGallery  2.0      BetterCredentials            A (compatible) major upgrade for Get-Credential, including support ...
PSGallery  5.0      Bing                         A few functions for working with the new Bing APIs                    
PSGallery  1.0.0.1  cFileShare                   The cFileShare module is a part of the ALM Ranger DevOps solutions ...
PSGallery  1.0.0.1  CimInventory                 Module that should help read data from CIM/WMI data from remote com...
PSGallery  1.0      CodeCraft                    CodeCraft helps you crank out the code.                               
PSGallery  0.0.0.4  ContosoAnalyzerRules         Script analyzer rules for Contoso.                                    
PSGallery  6.0      ConversionModule             a module that performs various unit conversions                       
PSGallery  1.0.0.7  DebugPx                      The DebugPx module provides a set of commands that make it easier t...
PSGallery  1.0.1    Discovery                    Open the Black Box                                                    
PSGallery  1.0.0.8  DoPx                         The DoPx module provides a rich set of commands that extend the aut...
PSGallery  1.0      EWS                          Module to enable automating mail-related tasks                        
PSGallery  1.4      EZOut                        Taking the Fight out of PowerShell Formatting                         
PSGallery  2.1.0.2  FAQ                          A Frequently Asked Questions (and answers) module as a demonstratio...
PSGallery  1.0.0.0  FormatPx                     FormatPx separates the formatting layer from the data layer in Powe...
PSGallery  0.2.1.0  Formulaic                    Because Math is Tedious, not Hard                                     
PSGallery  1.0.0.1  GenericMethods               The Invoke-GenericMethod function allows you to call generic .NET m...
PSGallery  0.4      GistProvider                 Gist-as-a-Package - OneGet PowerShell Provider to interop with Gith...
PSGallery  1.2.0.0  HardwareManagement           Out-of-band hardware management module                                
PSGallery  1.0      Heatmap                      Heatmap is a module to help you see how hot your servers are running  
PSGallery  1.0.0.8  HistoryPx                    HistoryPx uses proxy commands to add extended history information t...
PSGallery  1.0.1    HttpListener                 Creates a new HTTP Listener enabling a remote client to execute Pow...

# 中略

PSGallery  1.0      Winformal                    Write winforms informally in scripts with WinFormal                   
PSGallery  2.1      xActiveDirectory             Module with DSC Resources for Active Directory                        
PSGallery  0.1.0.0  xAdcsDeployment              The xCertificateServices module can be used to install or uninstall...
PSGallery  0.1.2    xAzure                       Module with DSC Resources for Azure Resources                         
PSGallery  1.0.0.0  xBitlocker                   This DSC Module allows you to configure Bitlocker on a single disk,...
PSGallery  0.1      XBPS                         Script Cmdlets to interact with the Xbox Debug APIs.                  
PSGallery  1.0.0    xChrome                      Module for installing the chrome                                      
PSGallery  1.2.1    xComputerManagement          The xComputerManagement module is a part of the Windows PowerShell ...
PSGallery  1.0.1    xCredSSP                     Module with DSC Resources for WSMan CredSSP.                          
PSGallery  1.1.2    xDatabase                    The xDatabase module is a part of the Windows PowerShell Desired St...
PSGallery  1.1      xDhcpServer                  The xDhcpServer module is a part of the Windows PowerShell Desired ...
PSGallery  1.0.0    xDismFeature                 Module with DSC Resources for Deployment Image Servicing and Manage...
PSGallery  1.0      xDnsServer                   The xDnsServer module is a part of the Windows PowerShell Desired S...
PSGallery  2.0      xDscDiagnostics              Module to help in reading details from DSC events                     
PSGallery  1.1.1.1  xDSCResourceDesigner         The xDscResourceDesigner module is a part of the Windows PowerShell...
PSGallery  1.0.1.0  xExchange                    This DSC Module allows you to configure many different properties o...
PSGallery  2.6.0.0  xEXOUserAvailability         xEXOUserAvailability can help you to get user availability from Off...
PSGallery  1.1.1    xFailOverCluster             The xFailOverCluster module is a part of the Windows PowerShell Des...
PSGallery  1.0.0    xFirefox                     Firefox Main module                                                   
PSGallery  2.1.1    xHyper-V                     The xHyper-V module is a part of the Windows PowerShell Desired Sta...
PSGallery  1.0.3    xHyper-VBackup               To be utilized with Windows Server Technical Preview for the purpos...
PSGallery  0.2.16.2 xJea                         Module with DSC Resources for Just Enough Admin (JEA).  Jea makes i...
PSGallery  1.0.0.0  xMySql                       The xMySql module is a part of the Windows PowerShell Desired State...
PSGallery  2.1.1.1  xNetworking                  Module with DSC Resources for Networking area                         
PSGallery  1.0.0    xOneGet                      DSC Resource for OneGet                                               
PSGallery  0.1.0.1  xPendingReboot               This module identifies pending reboots in Windows Server and acts o...
PSGallery  1.0.1    xPhp                         The xPhp module is a part of the Windows PowerShell Desired State C...
PSGallery  3.0.3.3  xPSDesiredStateConfiguration The xPSDesiredStateConfiguration module is a part of the Windows Po...
PSGallery  1.0.2    xRemoteDesktopAdmin          Module with DSC Resources for enabling adminsitrative Remote Deskto...
PSGallery  1.0.1    xRemoteDesktopSessionHost    The xRemoteDesktopSessionHost module is a part of the Windows Power...
PSGallery  1.0.0.1  xSafeHarbor                  Module containing example of creating safe environment using DSC an...
PSGallery  1.1.0.0  xSCDPM                       Module with DSC Resources for deployment and configuration of Micro...
PSGallery  1.1.0.0  xSCOM                        Module with DSC Resources for deployment and configuration of Micro...
PSGallery  1.1.0.0  xSCSMA                       Module with DSC Resources for deployment and configuration of Micro...
PSGallery  1.1.0.0  xSCSPF                       Module with DSC Resources for deployment and configuration of Micro...
PSGallery  1.1.0.0  xSCSR                        Module with DSC Resources for deployment and configuration of Micro...
PSGallery  1.1.0.0  xSCVMM                       Module with DSC Resources for deployment and configuration of Micro...
PSGallery  1.0.1    xSmbShare                    The xSmbShare module is a part of the Windows PowerShell Desired St...
PSGallery  1.1.3.1  xSqlPs                       SQL module.                                                           
PSGallery  1.1.0.0  xSQLServer                   Module with DSC Resources for deployment and configuration of Micro...
PSGallery  1.0.1    xSystemSecurity              The xSystemSecurity module is a part of the Windows PowerShell Desi...
PSGallery  1.3.2.2  xWebAdministration           Module with DSC Resources for Web Administration                      
PSGallery  1.0      xWindowsUpdate               Module with DSC Resources for Windows Update                          
PSGallery  0.0.0.1  xWinEventLog                 Configure Windows Event Logs                                          
PSGallery  1.0.0.0  xWordPress                   This module contains the xWordPressSite and xIisWordPressSite resou...

ね?便利。紹介を別途していますのでどぞどぞ。

tech.guitarrapc.com

今回のサンプル

今回はDSCResource を使って紹介します。

リソースの配置場所

DSCリソースの配置は、システムレベルのみとなります。リソースを設置するパスはこれだけです。

C:\Program Files\WindowsPowerShell\Modules

PowerShell v4 において、DSC は x64 のみの対応のため、C:\Program Files (x86)\WindowsPowerShell\Modulesは参照しません。

また、C:\Windows\System32\WindowsPowerShell\v1.0\Modules にも置きませんし、$env:UserProfile\Documents\WindowsPowerShell\Modulesも参照しません。

リソースのダウンロード

今回は、GraniResource を使うのでをクローンするか、Zipでダウンロードしてください。

ダウンロード解凍すると以下の構成になると思います。

f:id:guitarrapc_tech:20141211042724p:plain

ワンライナーでコピー

管理者で実行したPowerShell で一発コピーいいですね。

ダウンロードしたフォルダ直下で以下のワンラインーをPowerShell(管理者で起動)に入力すると、リソースがコピーされます。

f:id:guitarrapc_tech:20141211043327p:plain

マウスポチポチ

あるいは、マウスでポチポチなら以下のパスに移動して

DSCResources-master\Custom

f:id:guitarrapc_tech:20141211042843p:plain

GraniResourceフォルダを、C:\Program Files\WindowsPowerShell\Modulesにコピーします。

f:id:guitarrapc_tech:20141211042933p:plain

リソースが正しく設置できたか確認する

DSCリソースは、DSC用に拡張された PowerShell モジュールです。

そのため、2つの確認ができます。

モジュールとして読み込めるか

主に作成した時に、モジュールの構成として正しいかを確認する時に使います。公開されているものを利用するときは余り使わない....です、たぶん、きっと。

  • モジュールとしての構成に問題がないかの確認です

以下のコマンドがエラーなく完了すれば ok です。

f:id:guitarrapc_tech:20141211043839p:plain

バージョンも取れてますね。

f:id:guitarrapc_tech:20141211043921p:plain

ここまでが問題なければ、次にリソースとして読み込めるかを確認しましょう。

リソースとして読み込めるか

リソースを新たに追加したら必ず確認しましょう。

  • リソースとしての構成に問題ないかの確認です

リソースの詳細構成は後日説明しますが、MOFファイルのスキーマ構成や、.psm1の構成が正しくないと表示されません。

以下のコマンドで確認ができます。PowerShell v4では実行パフォーマンスがあり遅い*1のですが、PowerShellv5 では大幅に改善されています。*2

Module が GraniResource のものが見えれば正常に読み込めています。

f:id:guitarrapc_tech:20141211044606p:plain

リソースを使ったコンフィグレーションの構文

リソースには、それぞれ対応するコンフィグレーションの キーがありコンフィグレーション記述があります。リソースを設置したら見てみたいですよね?

では、GraniResourceのcACLリソースを使って、フォルダの権限を設定してみましょう。

リソースの読み込み

マイクロソフト標準リソース以外を利用する時は、コンフィグレーション構文の中で Import-DSCResource -ModuleName [ModuleName] と記述するとモジュールに含まれるリソースがそのコンフィグレーション構文中で利用できます。

続いて、コンフィグレーション構文に則って、リソース名を使ってあるべき状態を書いてみましょう。

まず リソース名を載せてみると、Ctrl * Space でインテリセンスが出ます。

f:id:guitarrapc_tech:20141211045342p:plain

[] でキーが囲まれていないものが、必須のキーです。[]でかかれているのはオプショナルです。

例えば、D:\test フォルダに、everyoneユーザーに対して FullControl の権限を与えるならこうです。

自分自身への Push 実行を行います。詳細は10日目にやるので説明は省略で!

f:id:guitarrapc_tech:20141211050453p:plain

結果は?うまくかかっていますね。

f:id:guitarrapc_tech:20141211050225p:plain

このような感じでカスタムリソースもさくっと使えるの大事です。

まとめ

Chef同様に、DSCもリソースで拡張されます。PowerShellの既存の仕組みに載せているので今までの経験が行かせるのも学習コストが小さくて楽だと思います。

では明日はいよいよ実行ですね!Push で試してみましょう。

*1:なんと20-30秒かかる悲惨さ

*2:1,2秒で応答します