Powershell でカスタム web.config セクションとその内容を取得する

Powershell でカスタム web.config セクションとその内容を取得する

c:\inetpub\wwwroot_Site1\AppName次のようなカスタム セクション グループとセクションを持つWeb アプリケーションがインストールされています。

<configSections>
  <sectionGroup name="Libraries">
    <section name="Custom.Section.Name" type="System.Configuration.NameValueSectionHandler,system, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, Custom=null"/>
    <section name="Custom.Section.Name2" type="System.Configuration.NameValueSectionHandler,system, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, Custom=null"/>
  </sectionGroup>
</configSections>

私は次の Powershell スニペットを作成しました:

Import-Module WebAdministration

Get-WebConfiguration //Libraries IIS:\Sites\Site1\AppName

正しくは以下が返されます:

名前 セクション グループ

==== ======== ===========

ライブラリ Custom.Section.Name

                  カスタム.セクション.名前2

私が理解できないのは、構成ファイルの実際の「本体」にある CustomSectionName の直接の子である要素にアクセスする方法、Get-WebConfigurationまたはアクセスを取得する方法です。Get-WebConfigurationProperty<add key="x" value="y" />

答え1

たまたま最近、私が作成した PowerShell Web フレームワークにこの関数を組み込みました。

必要な 3 つの行は次のとおりです。

Add-Type -AssemblyName System.Web
$webConfigStore = [Web.Configuration.WebConfigurationManager]::OpenWebConfiguration($path)              
$customSetting = $webConfigStore.AppSettings.Settings["$Setting"];   

3 番目は、何を取得しようとしているかによって多少異なります。

お役に立てれば

答え2

Web アプリケーションが SharePoint 2007 の場合、次の方法で web.config から単一の appSetting を選択できます。

param ( [string] $url='http://contso.com')

[System.Reflection.Assembly]::LoadWithPartialName('System.Web') | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SharePoint') | Out-Null

[Microsoft.SharePoint.SPSite] $site = New-Object -TypeName  'Microsoft.SharePoint.SPSite' -ArgumentList $url

[System.Configuration.Configuration] $config = [System.Web.Configuration.WebConfigurationManager]::OpenWebConfiguration('/', $site.WebApplication.Name)

<p># pull the one appSetting string we're interested in 

[string] $appSettingKey = 'avalidkey'

[string] $appSettingValue = $config.AppSettings.Settings[$appSettingKey].Value

Write-Host ("<appSetting> Key={0}, Value={1}" -f $appSettingKey, $appSettingValue)

$config = $null

$site.Dispose()

$site = $null

関連情報