Copy-Item:进程无法访问Power-Shell文件

时间:2017-02-20 06:06:49

标签: powershell jenkins

当我尝试将项目从路径复制到目标时,我会看到以下错误日志。这个PS脚本任务是在Jenkins Job下安排的。由于构建每次都失败,这就搞乱了。

错误日志 -

Copy-Item : The process cannot access the file 
'\\10.0.1.190\d$\Build\RPC\Fortius.RPC.AmadeusAir\Common.Logging.Core.dll' because it is being used by another process.
At C:\Users\Administrator\AppData\Local\Temp\hudson5254771699639808940.ps1:33 char:1
+ Copy-Item "$ReleaseDir\*" $AmadeusDir -Force -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Copy-Item], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand

PS脚本 -

# force strict - so any variable used before being assigned causes an error
Set-PsDebug -Strict

# force PowerShell to exit with a non-zero code on the first error
$ErrorActionPreference = 'Stop'

# set directories here once, so we can reuse
$AmadeusDir = "\\$env:SERVER\d$\Build\RPC\Fortius.RPC.AmadeusAir"
$ReleaseDir   = "C:\Amadeus\BTP\src\Fortius.Amadeus.Air.RPC.Host\bin\Release"

# get directory contents (are you expecting these to return to Jenkins?)
Get-ChildItem "$AmadeusDir\*"
Get-ChildItem "$ReleaseDir\*" 

# create the search directory if it doesn't exist
if (-not (Test-Path -Path $AmadeusDir -PathType Container)) { New-Item -Path $AmadeusDir -type directory -Force }

# get the service, but fail gracefully if it doesn't exist
$service = Get-Service -Name AmadeusAirWindowsService -Computername $env:SERVER -ErrorAction SilentlyContinue

# if we have a service, stop and delete it
if($service.Status)
{
    sc.exe \\$env:SERVER stop AmadeusAirWindowsService
    if ($LASTEXITCODE -ne 0) { throw "error stopping the service: $LASTEXITCODE" }
    Write-Host "AmadeusAirWindowsService STOPPED"
    sc.exe \\$env:SERVER delete AmadeusAirWindowsService
    if ($LASTEXITCODE -ne 0) { throw "error deleting the service: $LASTEXITCODE" }
    Write-Host "AmadeusAirWindowsService DELETED"
}

# copy release to search
Copy-Item "$ReleaseDir\*" $AmadeusDir -Force -Recurse

# (re)create the service
sc.exe \\$env:SERVER create AmadeusAirWindowsService start=auto DisplayName="Fortius Amadeus Air RPC Service"  binPath= D:\Build\RPC\Fortius.RPC.AmadeusAir\WorldVentures.Fortius.Amadeus.Air.RPC.Host.exe
if ($LASTEXITCODE -ne 0) { throw "error creating the service: $LASTEXITCODE" }
sc.exe \\$env:SERVER description AmadeusAirWindowsService "This service hosts Fortius Amadeus Air RPC service"
if ($LASTEXITCODE -ne 0) { throw "error adding description to service: $LASTEXITCODE" }
sc.exe \\$env:SERVER start AmadeusAirWindowsService
if ($LASTEXITCODE -ne 0) { throw "error starting the service: $LASTEXITCODE" }
Write-Host "AmadeusAirWindowsService STARTED"

作为我正在使用的替代品 xcopy "From" "destination" /k/e/d/Y要做到这一点

1 个答案:

答案 0 :(得分:0)

当目的地仍有正在使用的文件时,您正试图复制内容。你有没有检查过这个/这些文件被锁定了什么?你已经停止了我看到的服务,你真的检查过这是否成功了? SysInternals也有"处理"和"过程资源管理器"这可以检查保持文件锁定的内容。