2

I try to use powershell to deploy a build (Visual Studio) to multiple remote server. The problem is, that the powershellscript does not copy the folders like I intendet to. I use the following powershell command to copy the published website to the destinations.

Copy-Item -Path D:\Websites\$(ProjectName)\ -Recurse -Destination \\%RemoteServer%\D$\Websites\$(ProjectName)\ -Container -Exclude *.config -force

When the folder on the remoteserver does not exist, everything works fine. But if the folder exists, all files are copied into a new subfolder instead of overwriting the existing ones.

So in the first execution the destination is corrected and looks like this:

D:\Websites\TestWebsite\ {all files}

After a second copy, the destination of the copy is:

D:\Websites\TestWebsite\Publish\ {all files}

The variables ProjectName and RemoteServer are in both executions the same.

Twiebie
  • 412
  • 1
  • 7
  • 16
  • See here: https://stackoverflow.com/questions/6500320/post-build-event-execute-powershell – f6a4 Feb 11 '19 at 06:30
  • @f6a4 - the execution of the powershell script is working fine. Only the result is not the behavior I expected, because the copied files are note moved to the directory I want. – Twiebie Feb 11 '19 at 06:35
  • You cannot use powershell in VS like the command shell. You have to create a .ps1-Script and call the script as descibed in the link above. Read the answers completely. – f6a4 Feb 11 '19 at 06:43
  • @f6a4 I changed it to an external powershell script. But the behavior is the same. – Twiebie Feb 11 '19 at 06:53
  • 1
    You have to submit "$(Projectname)" and %Remoteserver% as parameter to the script (alternatively you can use [environment]::Remoteserver in powershell for the servername). To check if all is right, just write the complete paths to a text file ($path | Out-File "test.txt"). I'm sure that something is wrong with the parameters. – f6a4 Feb 11 '19 at 07:03
  • @f6a4 I already write them as parameters, the problem I think is, that the script is not working consistend. In the postbuild I got an array of servers to which I want to copy. But the external script works the same as powershell "Copy-Item -Path D:\Websites\$(ProjectName)\ -Recurse -Destination \\%RemoteServer%\D$\Websites\$(ProjectName)\ -Container -Exclude *.config -force" – Twiebie Feb 11 '19 at 07:18
  • @Twiebie I can simulate this in plain Powershell but I have no solution. I seem to recal having read about this behavior but I can't readilly find it. – Lieven Keersmaekers Feb 11 '19 at 07:39
  • 2
    Well, [here](https://stackoverflow.com/a/37394124/52598) you go – Lieven Keersmaekers Feb 11 '19 at 07:40
  • @LievenKeersmaekers ah ok, I get the problem then. Do you know what I need to modify to copy all files in the target folder? As mentioned, it copies to a sub-folder within the target dir. I am not so firm with powershell. So if I check if the folder exists bevor, how do I copy all files to the mentioned target dir? – Twiebie Feb 11 '19 at 07:46
  • 1
    I'd just use robocopy for this - it's purpose built for file copy operations and is very well documented. – henrycarteruk Feb 11 '19 at 08:57
  • @JamesC. Thanks - after using robocopy, everything works like intended ! – Twiebie Feb 11 '19 at 09:09

0 Answers0