2015-10-20 86 views
1

我目前能夠傳輸文件..但不知何故內容被遺漏。我認爲這個對象沒有被正確地返回,但無法弄清楚。 請幫忙嗎?Powershell - 從ForEach對象返回/傳輸對象

$folder1 = "<external server>" 
$folder2 = "<local path>" 

# Get all files under $folder1, filter out directories 
$firstFolder = Get-JFSChildItem $folder1 | Where-Object { -not $_.PsIsContainer } 

    $firstFolder | ForEach-Object { 
    # Check if the file, from $folder1, exists with the same path under $folder2 
    If (!(Test-Path($_.FullName.Replace($folder1, $folder2)))) 
    { 
     $fileSuffix = $_.FullName.TrimStart($folder1) 
     Write-Host "$fileSuffix is only in folder1" 
     Receive-JFSItem $fileSuffix -destination $folder2 
    } 

} 

錯誤:Receive-JFSItem:No such file;沒有這樣的文件。

錯誤:接收-JFSItem < < < < $ fileSuffix -destination $文件夾2

回答

1

您正在使用TrimStart錯誤。 TrimStart接受一個符號集從參數中修剪,並且您希望將文件夾名稱從其開頭修剪爲精確的字符串。您應該用$_.fullname中的空字符串替換$folder1

If (!(Test-Path($_.FullName.Replace($folder1, $folder2)))) 
{ 
    $fileSuffix = $_.FullName.Replace($folder1,"") 
    Write-Host "$fileSuffix is only in folder1" 
    Receive-JFSItem $fileSuffix -destination $folder2 
} 
+0

感謝,但文件的內容仍然沒有被傳輸。 錯誤:Receive-JFSItem:沒有這樣的文件;沒有這樣的文件。 錯誤:+ Receive-JFSItem <<<< $ fileSuffix -destination $ folder2 錯誤:+ CategoryInfo:NotSpecified:(:) [Receive-JFSItem],SftpException 錯誤:+ FullyQualifiedErrorId:Rebex.Net.SftpException,MVPSI.Commands .ReceiveJFSItemCommand –

+0

如果是這樣,請更好地閱讀'Receive-JFSItem'手冊,或許它需要文件的完整名稱,並且您試圖僅爲其提供基本名稱。試試'Receive-JFSItem $ _。fullName -destination $ folder2',或者只是'$ _'。 – Vesper