2009-09-03 158 views
3

我試圖讓PowerShell將文件從遠程計算機(我有管理權限通過AD)複製到本地計算機。 它在最奇怪的地方失敗。下面是腳本的一個片段:PowerShell:複製項目找不到路徑

$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存在的路徑。我瘋了。

我能做些什麼來解決它?

回答

7

Eeeeh ....好嗎?

如果我取代了線

Copy-Item $config -Destination $config_target_dir 

Copy-Item $config.FullName $config_target_dir 

突然奇蹟般地工作....

是怎麼回事?

+6

不要忘了你正在處理PS中的實際對象。一般來說,當您將對象傳遞給cmdlet時,cmdlet非常適合選擇合適的屬性。在這種情況下,您將System.IO.FileSystemInfo.FileInfo對象傳遞給Copy-Item cmdlet。我認爲該cmdlet可能默認使用.Name屬性,並且該副本無法提供足夠的信息。當您明確將.FullName屬性賦予該cmdlet時,它現在具有它所需的信息。 – EBGreen 2009-09-03 12:52:28

+0

嗯,這將解釋錯誤,但*不*錯誤的錯誤消息。爲什麼要報告目標位置不存在的錯誤? – AndreasKnudsen 2009-09-03 15:54:00

+0

複製項目執行時的當前工作目錄是什麼?我的猜測是D:\ ServerDeploy \ TestMachine1 \網站。就像EBGreen所說,Copy將$ config當作相對路徑。因此,它認爲(join-path(pwd)$ config.Name)*是完整的源位置。 – 2009-09-18 21:40:00