PowerShell:Copy-Item无法找到路径

And*_*sen 4 powershell copy-item

我正在尝试让PowerShell将文件从远程计算机(我通过AD拥有管理员权限)复制到本地计算机.它在最奇怪的地方失败了.这是脚本的片段:

    $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
    }
}
Run Code Online (Sandbox Code Playgroud)

它失败了

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
Run Code Online (Sandbox Code Playgroud)

路径D:\ServerDeploy\TestMachine1\website存在.我为此疯狂.

我该怎么办才能修复它?

And*_*sen 7

Eeeeh ......好吗?

如果我换了线

 Copy-Item $config -Destination  $config_target_dir
Run Code Online (Sandbox Code Playgroud)

 Copy-Item $config.FullName $config_target_dir
Run Code Online (Sandbox Code Playgroud)

它突然神奇地工作....

是什么赋予了?

  • 不要忘记你正在处理PS中的实际对象.一般来说,当您将对象传递给cmdlet时,cmdlet非常擅长选择要使用的正确属性.在这种情况下,您将System.IO.FileSystemInfo.FileInfo对象传递给Copy-Item cmdlet.我认为cmdlet可能默认使用.Name属性,但这不足以使副本生效.当您明确地将.FullName属性提供给cmdlet时,它现在具有所需的信息. (6认同)