PowerShell: Copy-Item Cannot Find Path

I am trying to get PowerShell to copy files from a remote computer (on which I have administrator rights through AD ) to the local computer. It fails in the strangest place. Here is the script fragment:

    $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
    }
}

Message Failed

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\websiteexists. I will lose my mind on this.

What can I do to fix this?

+3
source share
1 answer

Eeeeh .... OK?

If I replaced the string

 Copy-Item $config -Destination  $config_target_dir

from

 Copy-Item $config.FullName $config_target_dir

he unexpectedly magically worked ....

What gives?

+7
source

Source: https://habr.com/ru/post/1716754/


All Articles