我正在尝试使用Power
Shell修改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;"
这很好用.
现在,我想在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>
我想更改“DirectoryDatabaseName”节点.
使用:
Set-WebConfigurationProperty -pspath "IIS:\Sites\$Name\WS" -filter "applicationSettings" -name "test" -value "ProductDirectory.$Name"
我可以更改“root”节点(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'.
我正在使用以下PS:
Set-WebConfigurationProperty -pspath "IIS:\Sites\$Name\WS" -filter "applicationSettings/Product.Core.WebServices.Properties.Settings/setting[@name='DirectoryDatabaseName']" -name "test" -value "ProductDirectory.$Name"
甚至没有这个工作(相同的错误):
Set-WebConfigurationProperty -pspath "IIS:\Sites\$Name\WS" -filter "applicationSettings/Product.Core.WebServices.Properties.Settings" -name "test" -value "ProductDirectory.$Name"
所以它不知何故停止工作“一级以下”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"
这假设一个标准的web.config appSettings部分,如下所示:
<configuration> ... <appSettings> ... <add key="TestKey" value="TestValue"></add> ... </appSettings> ... </configuration>
在您的情况下,该值存储为文本节点而不是属性,因此完整的xpath将是/applicationSettings/Product.Core.WebServices.Properties.Settings/setting[@name=’DirectoryDatabaseName’] / value / text( )但是我找不到任何web.config文件的例子,其值存储为文本节点. IIS cmdlets的示例也未显示对text()的任何引用.
您可能必须设置过滤器以检索设置元素,然后将其设置为具有正确文本的值元素.示例再次没有说明如何执行此操作,但它们确实显示了如何使用哈希表来设置多个值,因此您可以找到一种方法使其工作.