用于复制和替换的 Powershell 脚本 web.config

Powershell script to Copy and Replace web.config

我想要一个 powershell 脚本将文件从源文件夹复制到中间文件夹,然后从中间文件夹复制到目标文件夹。在目标文件夹中已经有一个配置文件。所以想替换目标文件夹中的 web.config 文件。

这是我试过的:

第一次尝试:

$src = "C:\Websites\CTABS\CTABSEQA2014\Web.config" 
$dst = "C:14_VCI_TEMP\" 

Get-ChildItem $src -Filter "txt.*.test.*" -Recurse | % { 
    #Creates an empty file at the destination to make sure that subfolders exists 
    New-Item -Path $_.FullName.Replace($src,$dst) -ItemType File -Force 
    Move-Item -Path $_.FullName -Destination $_.FullName.Replace($src,$dst) -Force 
}

第二次尝试:

$src = "C:14_VCI_TEMP\Web.config" 
$dst = "C:\Websites\CTABS\CTABSEQA2014\"

Get-ChildItem $src -Filter "txt.*.test.*" -Recurse | % { 
    #Creates an empty file at the destination to make sure that subfolders exists 
    New-Item -Path $_.FullName.Replace($src,$dst) -ItemType File -Force 
    Move-Item -Path $_.FullName -Destination $_.FullName.Replace($src,$dst) -Force 
}

无需创建文件来检查文件夹是否存在,只需使用 Test-Path:

$src = "C:14_VCI_TEMP\Web.config" 
$dst = "C:\Websites\CTABS\CTABSEQA2014\"

if((Test-Path $dst) -and (Test-Path $src))
{
    Copy-Item -LiteralPath $src -Destination $dst -Force
}
else
{
    Write-Output "Folder or file does not exist"
}

这将首先检查路径和文件,如果它们存在,它将复制文件。