Search code examples
powershellcopy-item

PowerShell: Copy-Item Cannot find path


I'm trying to get PowerShell to copy files from a remote computer (on which I have admin rights through AD) to the local computer. It fails in the strangest place. Here's a snippet of the script:

    $configs = Get-ChildItem -Recurse -ErrorAction SilentlyContinue -Filter "*.config" $serverUNCPath 
foreach($config in $configs){
    $config_target_dir = $dest.Path + $config.Directory.FullName.Replace($serverUNCPath,"")
    if(Test-Path -Path $config_target_dir){
        Copy-Item $config -Destination  $config_target_dir
    }
}

It fails with the message

Cannot find path 'D:\ServerDeploy\TestMachine1\website\web.config' because it does not exist.
At :line:39 char:12
+           Copy-Item <<<<  $config -Destination  $config_target_dir

The path D:\ServerDeploy\TestMachine1\website exists. I'm going mad over this.

What can I do to fix it?


Solution

  • Eeeeh.... OK?

    If I replaced the line

     Copy-Item $config -Destination  $config_target_dir
    

    with

     Copy-Item $config.FullName $config_target_dir
    

    it suddenly magically worked....

    What gives?