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?
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With