2013-07-27 14 views
6

Desidero modificare il file di configurazione Web utilizzando powershell. sono bloccato da qualche parte Voglio aggiornare le impostazioni di app e anche le informazioni di connessione allo stesso tempo quando le cambio in powershelModifica del file di configurazione Web utilizzando Microsoft PowerShell

Ho questo codice ma cambia solo il valore apppsettings quando lo cambio qui ed eseguo ma voglio anche includere connectiontring qui. Come posso ottenerlo?

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

Ecco il mio web file di configurazione

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

qui voglio upadate appsettings - (valore chiave e) ed anche connectionStrings (nome e InitialCatalog) allo stesso tempo

quando ho provato il tuo codice mi dà questo errore

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 

risposta

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

È possibile utilizzare XPath per selezionare i nodi e impostarne il valore tramite la proprietà #text aggiunta da PowerShell.

Nota: il tuo esempio xml ha problemi con la custodia e alcuni refusi. Ecco cosa ho provato con:

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

Ciao, cosa dovrei scrivere nell'area #text? – user2375896

+1

@ user2375896 che in realtà dovrebbe essere '# testo'. PowerShell inserisce il valore del nodo XML in quella proprietà. Inoltre sto indovinando il tuo layout XML. Se pubblichi l'XML che hai e quello che vuoi aggiunto e dove posso rendere l'esempio migliore per te. –

+0

Sto usando la versione 4 di PowerShell e il "testo #" non ha funzionato per me. Sostituire ''# testo'' con' .value' sembra fare il trucco. – Darrel

Problemi correlati