Number of files processed by Copy-Item not working with –FromSession parameter?

Welcome Forums General PowerShell Q&A Number of files processed by Copy-Item not working with –FromSession parameter?

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
1 month, 1 week ago.

  • Author
    Posts
  • #121387

    Participant
    Points: 15
    Rank: Member

    Hi,

    I have the following script that works great and return the number of files processed by Copy-Item,
    however, if I use the -Fromsession parameter it will always return 0 files even though some of the  files
    have been processed successfully:

    Copy-Item –Path C:\ShadowCopy\temp\* –Destination D:\Test\ –Recurse -force -PassThru -ErrorAction silentlyContinue |
    Measure-Object | Select-Object -Expand Count |
    Tee-Object -Variable file_count | Out-Null
    
    if ($file_count -ge "1") {
    Write-Host "Number of files processed: $file_count"
    }
    else { 
    Write-Host $error[0].exception.message -ForegroundColor Red
    }

    This script is NOT working:

    $Session = New-PSSession -ComputerName PC_NAME -EA 1
    
    Copy-Item –Path C:\ShadowCopy\temp\* –FromSession $Session –Destination D:\Test\ –Recurse -force -PassThru -ErrorAction silentlyContinue |
    Measure-Object | Select-Object -Expand Count |
    Tee-Object -Variable file_count | Out-Null
    
    if ($file_count -ge "1") {
    Write-Host "Number of files processed: $file_count"
    }
    else { 
    Write-Host $error[0].exception.message -ForegroundColor Red
    
    }

     

    What am I missing?

     

     

  • #121398

    Participant
    Points: 885
    Helping Hand
    Rank: Major Contributor

    Copy item doesn't Passthru when copying from/to sessions.

  • #121401

    Participant
    Points: 9
    Rank: Member

    As a workaround do a Get-ChildItem on the local copy of the files and return the count

  • #121404

    Participant
    Points: 15
    Rank: Member

    Thanks, that explain why it is not working then.

You must be logged in to reply to this topic.