Powershell에서 사용자 지정 web.config 섹션 및 해당 콘텐츠 가져오기

Powershell에서 사용자 지정 web.config 섹션 및 해당 콘텐츠 가져오기

c:\inetpub\wwwroot_Site1\AppName다음과 같이 사용자 정의 섹션 그룹과 섹션이 있는 웹 애플리케이션이 설치되어 있습니다 .

<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

올바르게 반환되는 것은 다음과 같습니다.

이름 섹션 그룹

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

라이브러리 사용자 정의.섹션.이름

                  사용자 정의.섹션.이름2

내가 짐작할 수 없는 것은 구성 파일의 실제 "본문"에 있는 CustomSectionName의 직계 하위 요소를 통해 Get-WebConfiguration또는 Get-WebConfigurationProperty이에 대한 액세스를 얻는 방법입니다 .<add key="x" value="y" />

답변1

최근에 제가 작성하는 PowerShell 웹 프레임워크에 이 기능을 추가하게 되었습니다.

필요한 세 줄은 다음과 같습니다.

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

세 번째는 무엇을 얻으려는지에 따라 다소 달라질 수 있습니다.

도움이 되었기를 바랍니다

답변2

웹 응용 프로그램이 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

관련 정보