简体   繁体   English

Powershell-从ForEach-Object返回/传输对象

[英]Powershell - Returning/Transferring an object from ForEach-Object

I am currently able to transfer the file .. but somehow the content is being left out. 我目前能够传输文件..但不知何故内容被忽略了。 I think that object is not being returned properly but cannot figure it out. 我认为该对象未正确返回,但无法弄清楚。

$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
    }

}

Error: Receive-JFSItem : No such file; 错误:Receive-JFSItem:没有这样的文件; No such file. 没有这样的文件。

Error: Receive-JFSItem <<<< $fileSuffix -destination $folder2 错误:Receive-JFSItem <<<< $ fileSuffix -destination $ folder2

You are using TrimStart wrongly. 您错误地使用了TrimStart TrimStart accepts a set of symbols to trim from the argument, and you expect to trim the folder name as an exact string from its beginning. TrimStart接受一要从参数中修剪的符号 ,并且您希望从其开始就将文件夹名称修剪为精确的字符串。 You should instead replace $folder1 with empty string in $_.fullname . 而应该替换$folder1与空字符串$_.fullname

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM