[PowerShell] Copy-Item: Error Container cannot be copied onto existing leaf item

In my Jenkins pipeline, I found error on step copy build result to destination folder.

  • Some file is missing
  • Error On Copy-Item Command
Copy-Item : Container cannot be copied onto existing leaf item.
At D:\WorkSpace:[line] char:5
+     Copy-Item D:\\GITMAIN\\OFFICEADDINS D:\\OUTPUT\\NET472PATH\\Office-Addins\\ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (D:\\GITMAIN\\OFFICEADDINS :String) [Copy-Item], PSArgumentException
    + FullyQualifiedErrorId : CopyContainerItemToLeafError,Microsoft.PowerShell.Commands.CopyItemCommand

Root Cause

  • the destination path does not exist.

Solution

  • Create destination path if not exist
if ((Test-Path -PathType container "$BASE_NET472PATH\\Office-Addins\\"))
{
   #Create destination path if not exist
   if (!(Test-Path -path "$GBASEOUTPUT\\Office-Addins\\"))
   {
     New-Item "$GBASEOUTPUT\\Office-Addins\\" -Type Directory
   }
   echo "Copy Office-AddIns"
   Copy-Item "$BASE_NET472PATH\\Office-Addins\\*" -Destination "$GBASEOUTPUT\\Office-Addins" -recurse -Force
}

Reference


Discover more from naiwaen@DebuggingSoft

Subscribe to get the latest posts to your email.