Obtendo seções web.config personalizadas e seu conteúdo no Powershell

Obtendo seções web.config personalizadas e seu conteúdo no Powershell

Eu tenho um aplicativo da web instalado c:\inetpub\wwwroot_Site1\AppNameque possui um grupo de seções personalizadas e uma seção da seguinte forma:

<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>

Eu escrevi o seguinte trecho do Powershell:

Import-Module WebAdministration

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

O que retorna corretamente:

Nomear grupos de seções

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

Bibliotecas Custom.Section.Name

                  Custom.Section.Name2

O que não consigo entender é como Get-WebConfigurationobter Get-WebConfigurationPropertyacesso aos <add key="x" value="y" />elementos que são filhos diretos de CustomSectionName no "corpo" real do arquivo de configuração.

Responder1

Acontece que recentemente coloquei essa função em uma estrutura web do PowerShell que escrevo.

Aqui está o trio de linhas que você precisa:

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

O terceiro irá variar um pouco dependendo do que você está tentando obter.

Espero que isto ajude

Responder2

Se o aplicativo Web for do tipo SharePoint 2007, você poderá escolher um único appSetting em seu web.config por meio de:

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

informação relacionada