0

I have two consecutive calls to Copy-Item but the second one is not copying the file. Both are copying the same file to two different folders. Both folders exist. But the second one never copies. I tried commenting out the first one, but the second one still does not copy.

$deliveryFolder = "C:\Users\administrator.GTCS\Desktop\Delivery\1.5.6.1140\"    
$localFolder = "C:\WebServerGateway\"    
$fileSharingServerManifestFileName = "GTCS_GCS_FileSharingServer_Manifest.xml"

Copy-item -Path $localFolder$fileSharingServerManifestFileName -Destination $deliveryFolder"GcsData\Configurations\Platforms\DEV\Web Server Gateway" -Force

Copy-item -Path $localFolder$fileSharingServerManifestFileName -Destination $deliveryFolder"GcsData\Configurations\Platforms\INT\Web Server Gateway" -Force
3
  • Do you receive any error? Add -Verbose Commented Jun 4, 2021 at 14:35
  • Did you run Test-Path on them? Commented Jun 4, 2021 at 14:37
  • Is the second folder read only? Commented Jun 4, 2021 at 14:39

1 Answer 1

4

If you are going to combine strings in this manner you need to wrap them in double quotes, to expand the variables:

$deliveryFolder = "C:\Users\administrator.GTCS\Desktop\Delivery\1.5.6.1140\"    
$localFolder = "C:\WebServerGateway\"    
$fileSharingServerManifestFileName = "GTCS_GCS_FileSharingServer_Manifest.xml"

    Copy-item -Path "$localFolder$fileSharingServerManifestFileName" -Destination "$($deliveryFolder)GcsData\Configurations\Platforms\DEV\Web Server Gateway" -Force
    
    Copy-item -Path "$localFolder$fileSharingServerManifestFileName" -Destination "$($deliveryFolder)GcsData\Configurations\Platforms\INT\Web Server Gateway" -Force

Notice I uses a subexpression $() in the -Destination argument. Because in your case the parser may have trouble determining where the variable name ends, therefore the string may not expand properly.

That said, neither of the arguments are particularly pretty/readable so I'd advise against this syntax. concatenating or better yet using Join-Path to derive the arguments are better choices:

$Path = $localFolder + $fileSharingServerManifestFileName
$Dest = $deliveryFolder + "GcsData\Configurations\Platforms\INT\Web Server Gateway"

Note: Concatenation forces you to pay attention to backslashes separating what would be the -Parent/-Child arguments in Join-Path

Or:

$Path = Join-Path $localFolder $fileSharingServerManifestFileName
$Dest = Join-Path $deliveryFolder "GcsData\Configurations\Platforms\INT\Web Server Gateway"

Then simply modify the commands:

Copy-item -Path $Path -Destination $Dest -Force
Copy-item -Path $Path -Destination $Dest -Force
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.