Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell version 5 Copy-Item -FromSession cannot be found

I'm trying to copy some log files from a remote session via the -FromSession paramter of the Copy-Item cmdlet. On the calling machine I've PS version 5 installed. When running the script I get following error:

Copy-Item : A parameter cannot be found that matches parameter name 'FromSession'.

When I'm calling $PSVersionTable on the source machine I get following output:

PSVersion                      5.0.10586.672
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.10586.672
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

I also check the session state, it is opened:

$session | select State

State
-----
Opened

Script:

$globalTargets = @("machine1.domain", "machine2.domain")

function Copy-LogsFromRemotes {
   param (
      [Parameter(Mandatory=$true)]
      $Destination
   )

   $password = "xxxxx" | ConvertTo-SecureString -AsPlainText -Force
   $credentials = new-object -typename       System.Management.Automation.PSCredential -argumentlist "domain\user",$password

   foreach ($target in $globalTargets) {

     $session = New-PSSession -ComputerName $target -Credential $credentials
     if (!(Test-Path $Destination)){
         New-Item -Path $Destination -ItemType Directory
     }
     $copyDestination = ("{0}\{1}" -f $Destination, $session.ComputerName)

     if (!(Test-Path $copyDestination)){
        New-Item -Path $copyDestination -ItemType Directory
     }

     Invoke-Command -Session $session -ScriptBlock { Test-Path "D:\Logs"}
     Copy-Item -LiteralPath "D:\Logs" -Destination $copyDestination -Verbose -FromSession $session

     Remove-PSSession -Session $session
   }
}

Do I also need PS version 5 on the target machine? Actually PS version is installed on the target.

Any hints?

like image 685
Moerwald Avatar asked Mar 10 '23 13:03

Moerwald


1 Answers

You can come across this PowerShell 5.0 bug when you try to copy files from or to different drives. If the drive does not exist on the other system, this error occurs.

See: https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/11306682-bug-copy-item-fromsession-fails-if-local-machine

As a workaround you could:

  1. Copy the files to a temporary folder on the C:-drive (or another common drive)
  2. Copy those files to/from the remote machine
  3. Move the files to the final destination
like image 102
mhu Avatar answered Apr 28 '23 01:04

mhu