简体   繁体   中英

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; No such file.

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

You are using TrimStart wrongly. 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. You should instead replace $folder1 with empty string in $_.fullname .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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