
我安裝了一個 Web 應用程序,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
我無法理解的是如何透過Get-WebConfiguration
或Get-WebConfigurationProperty
取得對<add key="x" value="y" />
設定檔實際「主體」中 CustomSectionName 的直接子元素的存取權。
答案1
剛好我最近把這個功能放到了我寫的PowerShell Web框架中。
這是您需要的三行:
Add-Type -AssemblyName System.Web
$webConfigStore = [Web.Configuration.WebConfigurationManager]::OpenWebConfiguration($path)
$customSetting = $webConfigStore.AppSettings.Settings["$Setting"];
第三個會根據您想要獲得的內容而有所不同。
希望這可以幫助
答案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