【问题标题】:PowerShell: Copy-Item Cannot find pathPowerShell:复制项目找不到路径
【发布时间】:2010-11-25 04:54:44
【问题描述】:

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

    $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 存在。我要疯了。

我能做些什么来解决它?

【问题讨论】:

    标签: powershell copy-item


    【解决方案1】:

    Eeeeh.... 好吗?

    如果我换行了

     Copy-Item $config -Destination  $config_target_dir
    

     Copy-Item $config.FullName $config_target_dir
    

    它突然神奇地起作用了......

    什么给了?

    【讨论】:

    • 不要忘记您在 PS 中处理的是实际对象。一般来说,当您将对象交给 cmdlet 时,cmdlet 非常擅长选择要使用的正确属性。在这种情况下,您将 System.IO.FileSystemInfo.FileInfo 对象传递给 Copy-Item cmdlet。我认为 cmdlet 可能默认使用 .Name 属性,这不足以使副本正常工作。当您明确地将 .FullName 属性赋予 cmdlet 时,它现在拥有所需的信息。
    • 嗯,这可以解释错误,但不是糟糕的错误消息。为什么要报错目标位置不存在?
    • 执行 Copy-Item 时的当前工作目录是什么?我猜它是 D:\ServerDeploy\TestMachine1\website。就像 EBGreen 所说,Copy 将 $config 视为相对路径。因此,它认为 (join-path (pwd) $config.Name) 完整的源位置。
    猜你喜欢
    • 2013-07-08
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多