我正在尝试使用 PowerShell 修改 web.config。
到目前为止,我可以使用以下方法替换连接字符串:
Set-WebConfigurationProperty -pspath "IIS:\Sites\$Name\WS" -filter "connectionStrings/add[@name='Product.Core.WebServices.Properties.Settings.DirectoryConnectionString']" -name "connectionString" -value "Server=SERVERNAME\INSTANCE1;Database=ProductDirectory.$Name;Trusted_Connection=True;"
Run Code Online (Sandbox Code Playgroud)
这工作正常。
现在,我想更改 web.config 的 applicationSettings 部分中的某些内容。该部分看起来像:
<applicationSettings>
<Product.Core.WebServices.Properties.Settings>
<setting name="LogLevel" serializeAs="String">
<value>Info</value>
</setting>
<setting name="LogDirectory" serializeAs="String">
<value>D:\temp</value>
</setting>
<setting name="LogSpecialTroubleshoot" serializeAs="String">
<value>False</value>
</setting>
<setting name="LogSqlQueries" serializeAs="String">
<value>False</value>
</setting>
<setting name="LogPerformance" serializeAs="String">
<value>False</value>
</setting>
<setting name="LogXmlInput" serializeAs="String">
<value>False</value>
</setting>
<setting name="LogXmlInputSeperate" serializeAs="String">
<value>False</value>
</setting>
<setting name="EnableCatchAllLogging" serializeAs="String">
<value>True</value>
</setting>
<setting name="DirectoryDatabaseName" serializeAs="String">
<value>ProductDirectory</value>
</setting>
</Product.Core.WebServices.Properties.Settings>
</applicationSettings>
Run Code Online (Sandbox Code Playgroud)
我想更改“DirectoryDatabaseName”节点。
使用:
Set-WebConfigurationProperty -pspath "IIS:\Sites\$Name\WS" -filter "applicationSettings" -name "test" -value "ProductDirectory.$Name"
Run Code Online (Sandbox Code Playgroud)
我可以更改“根”节点(applicationSettings)并向其添加名为“test”的属性。但是,如果我试图“更深入”地了解结构,则会收到以下错误:
WARNING: Target configuration object 'applicationSettings/Product.Core.WebServices.Properties.Settings/setting[@name='DirectoryDatabaseName'] is not found a
t path 'MACHINE/WEBROOT/APPHOST/TestM/WS'.
Run Code Online (Sandbox Code Playgroud)
我为此使用了以下 PS:
Set-WebConfigurationProperty -pspath "IIS:\Sites\$Name\WS" -filter "applicationSettings/Product.Core.WebServices.Properties.Settings/setting[@name='DirectoryDatabaseName']" -name "test" -value "ProductDirectory.$Name"
Run Code Online (Sandbox Code Playgroud)
甚至这都不起作用(同样的错误):
Set-WebConfigurationProperty -pspath "IIS:\Sites\$Name\WS" -filter "applicationSettings/Product.Core.WebServices.Properties.Settings" -name "test" -value "ProductDirectory.$Name"
Run Code Online (Sandbox Code Playgroud)
所以不知何故它停止工作“低于一级” applicationSettings
此外,语法将如何在<value>和之间更改文本</value>?
在默认网站的虚拟目录中编辑 web.config 文件并尝试查看和更改 appSettings 部分时,以下对我有用:
#List all appSettings
Get-WebConfigurationProperty -pspath "iis:\Sites\Default Web Site\VirtualDir" -filter "/appSettings/add" -name *
#Update specific appSetting
Set-WebConfigurationProperty -pspath "iis:\Sites\Default Web Site\VirtualDir" -filter "/appSettings/add[@key='SomeKey']" -name value -Value "SomeValue"
Run Code Online (Sandbox Code Playgroud)
这假设有一个标准的 web.config appSettings 部分,如下所示:
<configuration>
...
<appSettings>
...
<add key="TestKey" value="TestValue"></add>
...
</appSettings>
...
</configuration>
Run Code Online (Sandbox Code Playgroud)
在您的情况下,该值存储为文本节点而不是属性,因此完整的 xpath 将是/applicationSettings/Product.Core.WebServices.Properties.Settings/setting[@name='DirectoryDatabaseName']/value/text()但是我找不到任何将值存储为文本节点的 web.config 文件示例。IIS cmdlet的示例也没有显示对 text() 的任何引用。
您可能必须设置过滤器以检索设置元素,然后将其设置为具有正确文本的值元素。同样,这些示例并未展示如何执行此操作,但它们确实展示了如何使用哈希表来设置多个值,因此您可以找到一种方法来使其工作。
| 归档时间: |
|
| 查看次数: |
13945 次 |
| 最近记录: |