【问题标题】:Editing Web Config file using Microsoft PowerShell使用 Microsoft PowerShell 编辑 Web Config 文件
【发布时间】:2014-06-11 18:29:24
【问题描述】:

我想使用 powershell 修改我的网络配置文件。我卡在某个地方。当我在 powershel 中更改它们时,我想同时更新 appsettings 和连接字符串信息

我有这段代码,但是当我在此处更改并运行它时,它仅更改 apppsettings 值,但我也想在此处包含连接字符串。我怎样才能实现它?

$webConfig = "C:\Inetpub\Wwwroot\application\web.config"
$doc = new-object System.Xml.XmlDocument
$doc.Load($webConfig)
$doc.get_DocumentElement()."appsetting".add[0].value = "true"
$doc.Save($webConfig)

这是我的网络配置文件

 <appSettings>
     <add key="mykey1" value="false"/>
     <add key="mykey2" value="true"/>
     <add key="mykey3" value="false"/>
</appSettings>

  <connectionstrings>

  <add name="myname1" connectinstring="Data Source=ABDULLAH-PC\SQLEXPRESS;Initial Catalog=UserDataBase;
  Integrated Security=True" providerName="System.Data.SqlClient" />
  <add name="myname2" connectinstring="myconnectionstring2" />
   <add name="myname3" connectinstring="myconnectionstring3" />
 </connectionStrings>

在这里我想更新 appsettings -(键和值)以及连接字符串(名称和初始目录) 同时

当我尝试你的代码时,它给了我这个错误

Property '#text' cannot be found on this object; make sure it exists and is settable.
At line:3 char:66
+ $doc.SelectSingleNode('//appSettings/add[@key="mykey1"]/@value'). <<<< '#text' = 'false'
+ CategoryInfo          : InvalidOperation: (#text:String) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound

Property '#text' cannot be found on this object; make sure it exists and is settable.
At line:4 char:85
+ $doc.SelectSingleNode('//connectionStrings/add[@name="myname1"]/@connectionstring'). <<<< '#text'='my_string'       
  + CategoryInfo          : InvalidOperation: (#text:String) [], RuntimeException
   + FullyQualifiedErrorId : PropertyNotFound

【问题讨论】:

    标签: c#-4.0 powershell powershell-2.0 powershell-3.0


    【解决方案1】:
    $webConfig = "C:\Inetpub\Wwwroot\application\web.config"
    $doc = (gc $webConfig) -as [xml]
    $doc.SelectSingleNode('//appSettings/add[@key="mykey1"]/@value').'#text' = 'true'
    $doc.SelectSingleNode('//connectionStrings/add[@name="myname1"]/@connectionstring').'#text' = 'my_string'
    $doc.Save($webConfig)
    

    您可以使用 XPath 选择节点并通过 PowerShell 添加的 #text 属性设置它们的值。

    注意 - 您的示例 xml 存在大小写问题和一些拼写错误。这是我测试过的:

    <root>
        <appSettings>
             <add key="mykey1" value="false"/>
        </appSettings>
        <connectionStrings>
            <add name="myname1" connectionstring="Data Source=ABDULLAH-PC\SQLEXPRESS;Initial Catalog=UserDataBase; Integrated Security=True" providerName="System.Data.SqlClient" />
        </connectionStrings>
    </root>
    

    【讨论】:

    • 嗨,我应该在#text区写什么?
    • @user2375896 实际上应该是#text。 PowerShell 将 XML 节点的值放在该属性中。我也在猜测你的 XML 布局。如果您发布您拥有的 XML 以及您想要添加的内容,以及我可以在哪里为您改进示例。
    • 我使用的是 powershell 版本 4,'#text' 对我不起作用。用.value 替换'#text' 似乎可以解决问题。
    • 我也在使用 PowerShell 版本 4。但是用 .value 替换 '#text' 不起作用。你能给我一个工作的例子吗?提前致谢。
    猜你喜欢
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多