简体   繁体   中英

Copy-Item : The process cannot access the file

I have a Jenkins job which does below activity.

  1. Stop WebService
  2. Delete WebService
  3. Copy items from Jenkins workspace to server path
  4. Create WebService
  5. Start WebService

Below is my PowerShell script:

Get-ChildItem "C:\Location\*"
$service = Get-Service -Name value -Computername $env:SERVER -ErrorAction SilentlyContinue
sc.exe \\$env:SERVER stop value
Write-Host "value STOPPED"
sc.exe \\$env:SERVER delete val
Write-Host "val DELETED"
Copy-Item "C:\Location\*" "\\$env:SERVER\d$\Location" -Force -Recurse
sc.exe \\$env:SERVER create val start=auto DisplayName ="val"  binPath= D:\Location.exe
sc.exe \\$env:SERVER description value"value"
sc.exe \\$env:SERVER start value
Write-Host "value STARTED" 
if ($error) { exit 1 }

Error logs:

Copy-Item : The process cannot access the file '\\Location' because it is being used by another process.
At C:\Users\Administrator\AppData\Local\Temp\hudson2059984936352103941.ps1:18 char:5
+     Copy-Item "C:\Location\*" " ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Copy-Item], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand

[SC] CreateService FAILED 1072:

The specified service has been marked for deletion.

[SC] ChangeServiceConfig2 FAILED 1072:

The specified service has been marked for deletion.

[SC] StartService FAILED 1058:

The service cannot be started, either because it is disabled or because it has no
enabled devices associated with it.

Can you please help me out with this error? Do I need to restart the deploy server so that my process gets killed? If so I feel this is not relevant and cannot do in prod servers.

The problem is that you have another process which is using the dll. Since it is being used you cannot remove it. You need to make sure that no process is using your dll before you can remove it. To achieve this you will need to find out which process is using it, why it is using it and make sure that it will be closed. If the problem occurs next time as well, then you will need to add closing that process to your script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM