Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a web application installed in c:\inetpub\wwwroot_Site1\AppName which has a custom section group and section as follows:

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

I've written the following snippet of Powershell:

Import-Module WebAdministration

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

Which correctly returns:

Name         Sections                           Groups

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

Libraries    Custom.Section.Name

                  Custom.Section.Name2

What I can't fathom is how to, either via Get-WebConfiguration or Get-WebConfigurationProperty obtain access to the <add key="x" value="y" /> elements that are direct children of CustomSectionName in the actual "body" of the configuration file.

share|improve this question
add comment

2 Answers

It just so happens that I recently put this function into a PowerShell web framework I write.

Here's the trio of lines you'll need:

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

The third will vary somewhat depending on what you're trying to get.

Hope this Helps

share|improve this answer
add comment

If the web application is of the SharePoint 2007 variety, you can pick off a single appSetting from its web.config via:

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.