使用Microsoft PowerShell编辑Web Config文件 [英] Editing Web Config file using Microsoft PowerShell

查看:248
本文介绍了使用Microsoft PowerShell编辑Web Config文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用powershell修改我的Web配置文件。我困在某个地方。当我在powershel中更改它们时,我想同时更新appsettings和connectionsstring信息。

i want to modify my web config file using powershell . i stuck in somewhere . i want to update appsettings and also connectionsstring information at the same time when i change them in powershel

我有此代码,但是当我在此处更改它时,它仅更改了apppsettings的值,运行它,但我也想在这里包括connectionstring。我如何实现它?

I have this code but it changes only apppsettings value when i change it here and run it but i also want to include connectionstring here. How can i achieve it ?

$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)

这是我的Web配置文件

Here is my web config file

 <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-(键和值)以及连接字符串(名称和初始目录)
同时

Here i want to upadate appsettings -( key and value) and also connectionstrings( name and initialcatalog) at the same time

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

when i tried your code it gives me this error

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


推荐答案

$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选择节点并通过设置XPath的值 #text 属性PowerShell添加。

You can use XPath to select your nodes and set their value via the #text property PowerShell adds.

注意-您的示例x​​ml的大小写和拼写错误。这是我测试的内容:

Note - your example xml has problems with casing and some typos. Here is what I tested with:

<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>

这篇关于使用Microsoft PowerShell编辑Web Config文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆