需要一个脚本将构建输出发布到登台服务器

时间:2012-11-15 15:17:28

标签: powershell powershell-v2.0

我正在尝试编写一个PowerShell脚本,该脚本将从源文件夹中复制文件的子集并将它们放入目标文件夹中。我一直在玩“复制项目”和“删除项目”半天,无法获得所需或一致的结果。

例如,当我多次运行以下cmdlet时,文件会在不同的位置结束?!?!:

copy-item -Path $sourcePath -Destination $destinationPath -Include *.dll -Container -Force -Recurse

我一直在尝试我能想到的各种选项和命令组合,但找不到合适的解决方案。因为我确信我没有做任何非典型的事情,所以我希望有人可以减轻我的痛苦并为我提供正确的语法。

源文件夹将包含大量具有各种扩展名的文件。例如,以下所有内容都是可能的:

  • 的.dll
  • .dll.config
  • .EXE
  • .exe.config
  • .lastcodeanalysisissucceeded
  • .PDB
  • .Test.dll
  • .vshost.exe
  • .XML

该脚本只需复制.exe,.dll和.exe.config文件,不包括任何.test.dll和.vshost.exe文件。我还需要脚本来创建目标文件夹(如果它们尚不存在)。

感谢任何让我前进的帮助。

2 个答案:

答案 0 :(得分:1)

尝试:

$source = "C:\a\*"
$dest =  "C:\b"

dir $source -include *.exe,*.dll,*.exe.config -exclude *.test.dll,*.vshost.exe  -Recurse | 
% {

 $sp = $_.fullName.replace($sourcePath.replace('\*',''), $destPath)

 if (!(Test-Path -path (split-path $sp)))
    {
     New-Item (split-path $sp) -Type Directory
    } 

    copy-item $_.fullname  $sp -force
  }

答案 1 :(得分:0)

只要文件在一个目录中,以下应该可以正常工作。它可能比需要的更冗长,但它应该是一个很好的起点。

$sourcePath = "c:\sourcePath"
$destPath = "c:\destPath"

$items = Get-ChildItem $sourcePath | Where-Object {($_.FullName -like "*.exe") -or ($_.FullName -like "*.exe.config") -or ($_.FullName -like "*.dll")}

$items | % {
    Copy-Item $_.Fullname ($_.FullName.Replace($sourcePath,$destPath))
}