-2

我正在开发一个项目,如果目标上已经存在相同的文件,部署工具会自动添加“.update”扩展名。例如

root
    web.config
    web.config.update
    connection.config
    connection.config.update

我想通过 powershell 执行以下部署后:

  1. 备份 *.config。
  2. 将现有的 *.config 替换为 *.update 文件。以下是所需的输出:

    根 web.config web.config.update connection.config connection.config.update 根 web.config connection.config 备份 web.config connection.config

有人可以帮助如何通过使用 powershell 实现上述目标吗?

4

1 回答 1

4

以下代码将执行您想要的。

  1. 将现有配置文件备份到基于当前日期命名的备份文件夹中。
  2. 通过删除现有配置文件并重命名更新文件来替换它们。

    $root_folder = 'c:\temp\root'
    
    # Create a backup folder 
    $backup_directory = New-Item -Path "$root_folder\backup_$(Get-Date -Format yyyyMMdd)" -Force -ItemType Directory
    
    Get-ChildItem -Filter *.config | ForEach-Object {
    
        # Copy .config files to the backup directory
        Copy-Item -Path $_.FullName -Destination "$($backup_directory.Fullname)" -Force
    
        # Delete the file from the source directory
        $_ | Remove-Item
    
        # Rename the .update files to .config files.
        Rename-Item -Path "$($_.FullName).update" -NewName $_.FullName -Force
    }
    
于 2013-05-13T08:43:37.450 回答