Modify web.config with powershell

后端 未结 2 1021
再見小時候
再見小時候 2021-01-17 18:34

I need to try to update a web.config file to change the IP address only of the web.config I have included the section of code Im looking at for powershell to script the chan

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-17 18:39

    You can try :

    $xml = [xml](Get-Content c:\temp\web.config)
    $conString = $xml.connectionStrings.add[0].connectionString
    $conString2 = $conString -replace '192.168.1.100','10.10.10.10'
    $xml.connectionStrings.add[0].connectionString = $conString2
    $conString = $xml.connectionStrings.add[1].connectionString
    $conString2 = $conString -replace '192.168.1.100','10.10.10.10'
    $xml.connectionStrings.add[1].connectionString = $conString2
    $xml.Save('c:\temp\web2.config')
    

    This do the job for the two connection strings. If you don't want to hard code the old IP address you can use :

    $conString -replace 'Server=.*;','Server=10.10.10.11;'
    

提交回复
热议问题