Powershell脚本:提示一个文件(通过掩码)并在命令行中使用该文件
免责声明:我对ps的了解不够,无法在合理的时间内完成这项工作,所以是的,我正在请其他人来完成我的肮脏工作 我希望能够在不打开命令行的情况下运行web.config转换 我的文件夹中有以下文件:Powershell脚本:提示一个文件(通过掩码)并在命令行中使用该文件,powershell,web-config-transform,Powershell,Web Config Transform,免责声明:我对ps的了解不够,无法在合理的时间内完成这项工作,所以是的,我正在请其他人来完成我的肮脏工作 我希望能够在不打开命令行的情况下运行web.config转换 我的文件夹中有以下文件: web.config - actual web config web.qa.config - web config transformation for qa env web.production.config - web config transformation for production env
web.config - actual web config
web.qa.config - web config transformation for qa env
web.production.config - web config transformation for production env
transform.ps1 - powershell script I want to use to run transformation
以下是我想要的:
PS文件应使用*.(?*?)\.config
枚举当前目录,并让我选择我感兴趣生成web.config的
。在我的示例中,我将看到两个选项:“qa”、“生产”
在我(用户)选择环境后(假设它是“qa”,所选环境存储为$env,相应的文件名存储为$transformation),脚本应执行以下操作:
web.config
备份为web.config.bak
.exe是一个基于XDT的工具,它从命令行运行web.config转换。好的,看起来很简单,我会帮你做一些肮脏的工作。;) 将以下内容另存为transform.ps1:
$environments = @()f
gci | %{if ($_ -match '.*\.(?<env>.*?)\.config') {$environments += $matches.env}}
Write-Host "`nEnvironments:"
for ($i = 0; $i -lt $environments.Length; $i++) {Write-Host "[$($i + 1)] $($environments[$i])"}
Write-Host
do {
$selection = [int](Read-Host "Please select an environment")
if ($selection -gt 0 -and $selection -le $environments.Length) {
$continue = $true
} else {
Write-Host "Invalid selection. Please enter the number of the environment you would like to select from the list."
}
} until ($continue)
$transformation = "web.$($environments[$selection - 1]).config"
if (Test-Path .\web.config) {
cpi .\web.config .\web.config.bak
} else {
Write-Warning "web.config does not exist. No backup will be created."
}
if ($?) {
Write-Host "`nApplying $transformation..."
[ctt][1].exe source:web.config transformation:$transformation destination:web.config preservewhitespaces verbose
Write-Host "Done.`n"
} else {
Write-Error "Failed to create a backup of web.config. Transformation aborted."
}
$environments=@()f
gci |%{if($\-match.*\.(?*?)\.config'){$environments+=$matches.env}
写入主机“`nEnvironments:”
对于($i=0;$i-lt$environments.Length;$i++){Write Host“[$($i+1)]$($environments[$i])”}
写主机
做{
$selection=[int](读取主机“请选择一个环境”)
if($selection-gt 0-和$selection-le$environments.Length){
$continue=$true
}否则{
写入主机“选择无效。请输入要从列表中选择的环境编号。”
}
}直至(续)
$transformation=“web.$($environments[$selection-1]).config”
if(测试路径。\web.config){
cpi.\web.config.\web.config.bak
}否则{
写入警告“web.config不存在。将不创建备份。”
}
如果($?){
写入主机“`napping$transformation…”
[ctt][1].exe源:web.config转换:$transformation目标:web.config保留空白详细信息
写主机“完成,`n”
}否则{
写入错误“未能创建web.config的备份。转换已中止。”
}
那么,这个脚本对你有用吗?(我不是在暗示什么……)
$environments = @()f
gci | %{if ($_ -match '.*\.(?<env>.*?)\.config') {$environments += $matches.env}}
Write-Host "`nEnvironments:"
for ($i = 0; $i -lt $environments.Length; $i++) {Write-Host "[$($i + 1)] $($environments[$i])"}
Write-Host
do {
$selection = [int](Read-Host "Please select an environment")
if ($selection -gt 0 -and $selection -le $environments.Length) {
$continue = $true
} else {
Write-Host "Invalid selection. Please enter the number of the environment you would like to select from the list."
}
} until ($continue)
$transformation = "web.$($environments[$selection - 1]).config"
if (Test-Path .\web.config) {
cpi .\web.config .\web.config.bak
} else {
Write-Warning "web.config does not exist. No backup will be created."
}
if ($?) {
Write-Host "`nApplying $transformation..."
[ctt][1].exe source:web.config transformation:$transformation destination:web.config preservewhitespaces verbose
Write-Host "Done.`n"
} else {
Write-Error "Failed to create a backup of web.config. Transformation aborted."
}