PowerShell:复制项目无法找到路径

6
我想让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
    }
}

它会失败并显示消息。
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

路径D:\ServerDeploy\TestMachine1\website存在。我对此感到非常沮丧。

我应该怎么做才能解决这个问题?

2个回答

11
Eeeeh.... OK?
如果我替换了这行代码
 Copy-Item $config -Destination  $config_target_dir

使用

 Copy-Item $config.FullName $config_target_dir

出乎意料地,它神奇地起作用了...

怎么回事?


8
请记住,在 PowerShell 中你正在处理实际对象。通常情况下,当你将一个对象交给命令时,它们很擅长选择要处理的正确属性。在这种情况下,你正在将一个 System.IO.FileSystemInfo.FileInfo 对象交给 Copy-Item 命令。我认为这个命令默认使用 .Name 属性,并且这并不足够让复制工作。当你明确地将 .FullName 属性提供给命令时,它现在拥有所需的信息。 - EBGreen
嗯,那可以解释错误,但是不能解释糟糕的错误信息。为什么要报告目标位置不存在的错误呢? - AndreasKnudsen
Copy-Item 执行时的当前工作目录是什么?我的猜测是 D:\ServerDeploy\TestMachine1\website。像 EBGreen 说的那样,Copy 把 $config 视为相对路径。因此,它认为 (join-path (pwd) $config.Name) 是完整的源位置。 - Richard Berg

0
在我的情况下,事实证明这是一个大小写敏感的问题。我的源路径在WSL中,而WSL是大小写敏感的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接