4

Using the PowerShell command Copy-Item to copy files works fine locally, but when running it in a PSSession, the options -Filter, -Include and -Exclude show no effect while copying files from the remote host to the local machine. I am running PowerShell 5.1 both on the local and the remote machine.

Here, the examples:

  • Local --> local: works fine (i.e. it copies only txt-files)

    Copy-Item -Path C:\run\* -Filter *.txt -Destination C:\test\
    
  • Remote --> local: does not work correctly (disregards from the filter settings and copies all files)

    $sess = New-PSSession -ComputerName <ComputerName> -Credential $cred
    Copy-Item -FromSession $sess -Path C:\run\* -Filter *.txt -Destination C:\test\
    

    The same holds when using -Include *.txt or -Exclude *.csv.

  • Local --> remote: works fine (copiyng only txt-files):

    $sess = New-PSSession -ComputerName <ComputerName> -Credential $cred
    Copy-Item -ToSession $sess -Path C:\run\* -Filter *.txt -Destination C:\test\ 
    

Thanks for any hints on what I am doing wrong!

Rantanplan
  • 180
  • 8
  • 1
    Similar issue at this link with no solution: https://powershell.org/forums/topic/copy-item-from-pssession/ – derekbaker783 Jul 02 '20 at 13:37
  • Not that it answers the question, but one solution would be to use ```Enter-PSSession``` to run the Local --> Remote copy from your current remote. So, get a session on the remote box, then from that session, get a session on your local box, and then run the local -> remote copy from your question. – derekbaker783 Jul 02 '20 at 13:45
  • I want to avoid `Enter-PSSession` because the script is supposed to be run without user interaction. Should have said that before, sorry. – Rantanplan Jul 06 '20 at 08:58
  • 1
    Here, our current **workaround** so far (zipping with filter settings, copying, unzipping): first `Invoke-Command -Session $sess -Scriptblock {Get-ChildItem C:\onremote\* -Include *.txt | Compress-Archive -DestinationPath "C:\onremote\textfiles.zip" -Force}`, then, `Copy-Item "C:\onremote\textfiles.zip" -Destination "C:\onlocal\" -FromSession $sess -Recurse -Force`, and finally `Expand-Archive -Path "C:\onlocal\textfiles.zip" -DestinationPath "C:\onlocal\" -Force`. – Rantanplan Jul 27 '20 at 19:25

1 Answers1

0

We all have come to know that with PowerShell there are multiple ways to do X or Y. Why not just use Invoke-Command?

$sess = New-PSSession -ComputerName <ComputerName> -Credential $cred

Invoke-Command -Session $sess -ScriptBlock {
    Copy-Item -Path 'C:\run\*' -Filter '*.txt' -Destination 'C:\test\'
}

Or just directly

Invoke-Command -ComputerName $ServerName -Credential $cred -ScriptBlock { 
    Copy-Item -Path 'C:\run\*' -Filter '*.txt' -Destination 'C:\test\' 
}
postanote
  • 15,138
  • 2
  • 14
  • 25
  • This will copy files from one place on the remote to another place on the remote.applying the filters correctly. However, I would like to copy them from the remote to my local machine with the filters working. So far, we are using a workaround where we first zip the files we want and then copying the resulting zip file and uncompressing it on the local machine: see my comment above. – Rantanplan Jul 27 '20 at 19:20