I have a script to copy all artifact dotnet to remote server (from Jenkins server to IIS folder on remote server) for Deploy Stage like below:
#Parameters
param($userName, $passWord, $serverName, $deployFolder, $destinationFolder, $iisName)
$passwd = ConvertTo-SecureString $passWord -AsPlainText -Force
$creds = New-Object -typename System.Management.Automation.PSCredential -argumentlist $userName, $passwd
$session = New-PSSession -ComputerName $serverName -Credential $creds
#Stop IIS Application
Invoke-Command -Session $session -ScriptBlock { Import-Module IISAdministration; Stop-Website $args[0];} -ArgumentList $iisName
Start-Sleep -s 200
#Copy new build to deploy folder
Copy-Item -Recurse -Force -Path $deployFolder -Destination $destinationFolder -ToSession $session
#Start IIS Application
Invoke-Command -Session $session -ScriptBlock { Import-Module IISAdministration; Start-Website $args[0];} -ArgumentList $iisName
But sometimes I got issue Copy-Item : The process cannot access the file 'dll' because it is being used by another process.
Any ideas? Please let me know!
Thanks
As comments of Bruce Zhang, I add stop app pool to my script. It works.
Stop-WebAppPool -Name $args[0];