PowerShell スナップイン: 構成セクションでの簡単な設定の変更

発行日 : 2008 年 4 月 14 日 (作業者 : thomad(英語))
更新日 : 2009 年 3 月 9 日 (作業者 : thomad(英語))

前述のチュートリアルでは、サイト、アプリケーション プール、アプリケーション、仮想ディレクトリなどの IIS 名前空間コンテナを管理する方法について説明しました。このチュートリアルでは、IIS 名前空間を通して公開されない構成設定を管理します。


はじめに

IIS 名前空間を使用して構成できない IIS 設定 (つまり、組み込みのコマンドレットでは変更できない IIS 設定) を変更できるコマンドレットがいくつかあります。IIS で提供されるコマンドレットを代わりに使用する必要があります。このチュートリアルでは、前述のチュートリアルで作成したサイト、アプリケーション、および仮想ディレクトリを使用します。

Get-WebConfiguration と Get-WebConfigurationProperty

Get-WebConfiguration と Get-WebConfigurationProperty を使用して、IIS 構成セクションを取得できます。これらは、Get-Item と Get-ItemProperty の組み合わせと非常によく似ています。Get-Item* は名前空間コンテナ (Sites、Apps、AppPools、VDirs) に対してのみ機能しますが、Get-WebConfiguration* はどの IIS 構成セクションに対しても機能します。

構成設定のクエリ

前に作成した DemoApp アプリケーションで有効になっている directoryBrowse セクションの設定について見ていきます。まず DemoApp フォルダーに移動し、次にこのフォルダー内で認証設定のクエリを実行します。これを実行する方法は次のとおりです。

PS IIS:\> cd IIS:\Sites\DemoSite\DemoApp
PS IIS:\Sites\DemoSite\DemoApp> dir
Type               Name                             Physical Path
----               ----                             -------------
file               Default.htm                      C:\DemoSite\DemoApp\Default.htm
virtualDirectory   DemoVirtualDir2                  C:\DemoSite\DemoVirtualDir2

次の例では、-filter パラメーターを使用して目的の構成セクションを指定し、-name パラメーターを使用して参照するプロパティを指定します。現在の場所ではないセクションの設定を参照する場合は、-PSPath プロパティを追加することもできます。既定の Web サイトのディレクトリ参照の設定をクエリする例は、次のとおりです。

Get-WebConfigurationProperty -filter /system.webServer/directoryBrowse -name enabled -PSPath 'IIS:\Sites\Default Web Site'
ItemXPath                   : /system.webServer/directoryBrowse
IsInheritedFromDefaultValue : False
IsProtected                 : False
Name                        : enabled
TypeName                    : System.Boolean
Schema                      : Microsoft.IIs.PowerShell.Framework.ConfigurationAttributeSchema
Value                       : False
IsExtended                  : False

Set-WebConfigurationProperty の使用

設定を変更するのも簡単です。

ロックされたセクションの処理

ここで問題が生じます。通常、認証セクションはロックされています。したがって、web.config ファイルに対して認証セクションを書き込むことはできません。代わりに中央の applicationhost.config ファイルに書き込む必要があります。下記のコマンドを使用して WindowsAuthentication を有効にしようとすると、ロック違反で失敗することがあります。

PS IIS:\Sites\DemoSite\DemoApp> Set-WebConfigurationProperty -filter /system.webServer/security/authentication/windowsAuthentication -name enabled -value true
Set-WebConfigurationProperty : This configuration section cannot be used at this path. This happens
 when the section is locked at a parent level. Locking is either by default (overrideModeDefault="D
eny"), or set explicitly by a location tag with overrideMode="Deny" or the legacy allowOverride="fa
lse".
At line:1 char:29
+ Set-WebConfigurationProperty  <<<< -filter /system.webServer/security/authentication/windowsAuthentication -name enabled -value true

ここで必要なことは、-PSPath と -location パラメーターを使用することです。次のコマンドを実行すると、アプリケーション DemoApp で Windows 認証が有効になります。location タグを使用していますが、構成の書き込み先は applicationhost.config になります。ロックと location タグの詳細については、こちらをクリックしてください。

PS IIS:\Sites\DemoSite\DemoApp> Set-WebConfigurationProperty -filter /system.webServer/security/authentication/windowsAuthentication -name enabled -value true -PSPath IIS:\ -location DemoSite/DemoApp

ただし、構成のクエリでは場所を指定する必要はありません。標準の Get-WebConfigurationProperty コマンドによって、設定が有効になっていることが示されます。

PS IIS:\Sites\DemoSite\DemoApp> Get-WebConfigurationProperty -filter /system.webServer/security/authentication/windowsAuthentication -name enabled
True

Get-WebConfiguration と Get-WebConfigurationProperty の比較

前述の例の Get-Item と Get-ItemProperty の違いが、ここでも当てはまります。Get-WebConfiguration は、プロパティだけではなく、セクション全体を取得します。これにより、変数にセクションを格納し、そのセクション内のプロパティに対して変更を加え、Set-WebConfiguration を使用してセクションを元の場所に保存できます。また、コマンド補完を活用することもできます。

次に例を示します。そのままコピーして貼り付けないでください。windowsAuthentication セクションのプロパティを調べます。「$winAuth.」と入力し、Tab キーを押します。利用可能なプロパティおよび機能が繰り返し表示されます。

PS IIS:\Sites\DemoSite\DemoApp> $winAuth = Get-WebConfiguration -filter /system.webServer/security/authentication/windowsAuthentication 
PS IIS:\Sites\DemoSite\DemoApp> $winAuth.enabled = $false
PS IIS:\Sites\DemoSite\DemoApp> $winAuth | set-Webconfiguration -filter /system.webServer/security/authentication/windowsAuthentication -PSPath IIS:\ -location "DemoSite/DemoApp"

Add-WebConfiguration

Add-WebConfiguration は、IIS 構成コレクションに何かを追加する必要がある場合に使用するコマンドレットです。IIS では、ハンドラー、モジュール、既定のドキュメント設定などに関して、コレクションを使用して複数の値を格納しています。

DemoApp の既定のドキュメント コレクションに新しい既定のドキュメントを追加する方法の例を示します。

PS IIS:\Sites\DemoSite\DemoApp<Add-WebConfiguration /system.webServer/defaultDocument/files  "IIS:\sites\Default Web Site" -at 0 -value
@{value="new-index.html"}

この例では、追加のパラメーターとして -at を使用しています。このパラメーターによって、コレクション内で新しい値を追加する場所を指定できます。0 は先頭を、-1 は末尾を意味します。

まとめ

このチュートリアルでは、IIS で提供されている Web 構成コマンドレットを使用する方法について説明しました。構成設定をクエリする方法、location タグを使用して設定を構成する方法、コマンド ライン補完を利用する方法、およびコレクションにエントリを追加する方法について学びました。

次のチュートリアルでは、グロビングや XPath などの高度な IIS プロバイダー機能を使用して、複雑な構成タスクを実行する方法について説明します。