对于那些希望在构建服务器中使用 PowerShell 脚本的人,这里有一个工作示例(至少在我的构建服务器上;))示例:
param
(
[string] $buildConfiguration = "Debug",
[string] $outputFolder = $PSScriptRoot + "\[BuildOutput]\"
)
Write-Output "Copying all build output to folder '$outputFolder'..."
$includeWildcards = @("*.dll","*.exe","*.pdb","*.sql")
$excludeWildcards = @("*.vshost.*")
# create target folder if not existing, or, delete all files if existing
if(-not (Test-Path -LiteralPath $outputFolder)) {
New-Item -ItemType Directory -Force -Path $outputFolder | Out-Null
# exit if target folder (still) does not exist
if(-not (Test-Path -LiteralPath $outputFolder)) {
Write-Error "Output folder '$outputFolder' could not be created."
Exit 1
}
} else {
Get-ChildItem -LiteralPath $outputFolder -Include * -Recurse -File | foreach {
$_.Delete()
}
Get-ChildItem -LiteralPath $outputFolder -Include * -Recurse -Directory | foreach {
$_.Delete()
}
}
# find all output files (only when in their own project directory)
$files = @(Get-ChildItem ".\" -Include $includeWildcards -Recurse -File |
Where-Object {(
$_.DirectoryName -inotmatch '\\obj\\' -and
$_.DirectoryName -inotmatch '\\*Test*\\' -and
$_.DirectoryName -ilike "*\" + $_.BaseName + "\*" -and
$_.DirectoryName -ilike "*\" + $buildConfiguration
)}
)
# copy output files (overwrite if destination already exists)
foreach ($file in $files) {
Write-Output ("Copying: " + $file.FullName)
Copy-Item $file.FullName $outputFolder -Force
# copy all dependencies from folder (also in subfolders) to output folder as well (if not existing already)
$dependencies = Get-ChildItem $file.DirectoryName -Include $includeWildcards -Exclude $excludeWildcards -Recurse -File
foreach ($dependency in $dependencies) {
$dependencyRelativePathAndFilename = $dependency.FullName.Replace($file.DirectoryName, "")
$destinationFileName = Join-Path -Path $outputFolder -ChildPath $dependencyRelativePathAndFilename
if (-not(Test-Path -LiteralPath $destinationFileName)) {
Write-Output ("Copying: " + $dependencyRelativePathAndFilename + " => " + $destinationFileName)
# create sub directory if not exists
$destinationDirectory = Split-Path $destinationFileName -Parent
if (-not(Test-Path -LiteralPath $destinationDirectory)) {
New-Item -Type Directory $destinationDirectory
}
Copy-Item $dependency.FullName $destinationDirectory
} else {
Write-Debug ("Ignoring (existing destination): " + $dependency.FullName)
}
}
}
这是 PowerShell 构建步骤中使用的脚本: