用于备份和替换文件的Powershell脚本

用于备份和替换文件的Powershell脚本,powershell,powershell-3.0,Powershell,Powershell 3.0,我正在处理一个项目,如果目标上已经存在相同的文件,部署工具会自动添加“.update”扩展名。 e、 g 我想通过powershell执行以下后期部署: 备份*.config 将现有的*.config替换为*.update文件。 以下是所需的输出: 根 web.config web.config.update connection.config connection.config.update 根 web.config connection.config 备份 web.config connec

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

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

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

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


  • 是否有人可以帮助您了解如何使用powershell实现上述功能?

    以下代码将执行您想要的功能

  • 将现有配置文件备份到名为“基于”的备份文件夹 当前日期
  • 通过删除现有配置文件并重命名更新文件来替换它们

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

  • 问题是什么?我想知道为什么你的问题被否决了。我想你可以重新措辞,使它更好:1。我有以下要求,等等。我如何使用Powershell实现这一点?谢谢,我的错,我错过了记事本中复制粘贴的最后一行++
    $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
    }