I'd like to use PowerShell as part of our automated build process to update an App.config file while deploying into our test environment. How can I do this?
我想在部署到我们的测试环境中时使用PowerShell作为自动构建过程的一部分来更新App.config文件。我怎样才能做到这一点?
2 个解决方案
#1
28
Given this sample App.config: C:\Sample\App.config:
鉴于此示例App.config:C:\ Sample \ App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="dbConnectionString"
connectionString="Data Source=(local);Initial Catalog=Northwind;Integrated Security=True"/>
</connectionStrings>
</configuration>
The following script, C:\Sample\Script.ps1, will read and write a setting:
以下脚本C:\ Sample \ Script.ps1将读取和写入设置:
# get the directory of this script file
$currentDirectory = [IO.Path]::GetDirectoryName($MyInvocation.MyCommand.Path)
# get the full path and file name of the App.config file in the same directory as this script
$appConfigFile = [IO.Path]::Combine($currentDirectory, 'App.config')
# initialize the xml object
$appConfig = New-Object XML
# load the config file as an xml object
$appConfig.Load($appConfigFile)
# iterate over the settings
foreach($connectionString in $appConfig.configuration.connectionStrings.add)
{
# write the name to the console
'name: ' + $connectionString.name
# write the connection string to the console
'connectionString: ' + $connectionString.connectionString
# change the connection string
$connectionString.connectionString = 'Data Source=(local);Initial Catalog=MyDB;Integrated Security=True'
}
# save the updated config file
$appConfig.Save($appConfigFile)
Execute the script:
执行脚本:
PS C:\Sample> .\Script.ps1
Output:
输出:
name: dbConnectionString
connectionString: Data Source=(local);Initial Catalog=Northwind;Integrated Security=True
Updated C:\Sample\App.config:
更新了C:\ Sample \ App.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="dbConnectionString"
connectionString="Data Source=(local);Initial Catalog=MyDB;Integrated Security=True" />
</connectionStrings>
</configuration>
#2
24
The code can be much more shorter (based on Robin's app.config):
代码可以更短(基于Robin的app.config):
$appConfig = [xml](cat D:\temp\App.config)
$appConfig.configuration.connectionStrings.add | foreach {
$_.connectionString = "your connection string"
}
$appConfig.Save("D:\temp\App.config")
#1
28
Given this sample App.config: C:\Sample\App.config:
鉴于此示例App.config:C:\ Sample \ App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="dbConnectionString"
connectionString="Data Source=(local);Initial Catalog=Northwind;Integrated Security=True"/>
</connectionStrings>
</configuration>
The following script, C:\Sample\Script.ps1, will read and write a setting:
以下脚本C:\ Sample \ Script.ps1将读取和写入设置:
# get the directory of this script file
$currentDirectory = [IO.Path]::GetDirectoryName($MyInvocation.MyCommand.Path)
# get the full path and file name of the App.config file in the same directory as this script
$appConfigFile = [IO.Path]::Combine($currentDirectory, 'App.config')
# initialize the xml object
$appConfig = New-Object XML
# load the config file as an xml object
$appConfig.Load($appConfigFile)
# iterate over the settings
foreach($connectionString in $appConfig.configuration.connectionStrings.add)
{
# write the name to the console
'name: ' + $connectionString.name
# write the connection string to the console
'connectionString: ' + $connectionString.connectionString
# change the connection string
$connectionString.connectionString = 'Data Source=(local);Initial Catalog=MyDB;Integrated Security=True'
}
# save the updated config file
$appConfig.Save($appConfigFile)
Execute the script:
执行脚本:
PS C:\Sample> .\Script.ps1
Output:
输出:
name: dbConnectionString
connectionString: Data Source=(local);Initial Catalog=Northwind;Integrated Security=True
Updated C:\Sample\App.config:
更新了C:\ Sample \ App.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="dbConnectionString"
connectionString="Data Source=(local);Initial Catalog=MyDB;Integrated Security=True" />
</connectionStrings>
</configuration>
#2
24
The code can be much more shorter (based on Robin's app.config):
代码可以更短(基于Robin的app.config):
$appConfig = [xml](cat D:\temp\App.config)
$appConfig.configuration.connectionStrings.add | foreach {
$_.connectionString = "your connection string"
}
$appConfig.Save("D:\temp\App.config")