2015-10-20 88 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