问题
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?
回答1:
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:
- Copy the files to a temporary folder on the C:-drive (or another common drive)
- Copy those files to/from the remote machine
- Move the files to the final destination
回答2:
I've also seen this same error come up when you place a wildcard in the source filename such as c:\*.log
. According to the docs, the -Path parameter does not accept wildcards.
回答3:
It seems to be a bug in PowerShell. What worked for me was ensuring that the remote file (e.g. D:\remotefolder\mylog.log) which you are trying to copy already exists on your local machine (exact same drive and path).
UPDATE: Bug seems to be fixed (at least in in 5.1.14409.1018)
# Workaround PowerShell bug by creating local dummy copy of remote file we want to fetch
"" | Set-Content "D:\remotefolder\mylog.log"
# Copy down remote file, overwriting dummy copy with -Force
Copy-Item -FromSession $app01 -Path "D:\remotefolder\mylog.log" -Destination "D:\localfolder\" -Force
Of course you have to create D:\remotefolder
locally beforehand as well.
If you don't have the same drive letter locally and remotely this will not work. You would have to make sure that you just do.
回答4:
When I get that error, I have to specify the full source path.
来源:https://stackoverflow.com/questions/40840269/powershell-version-5-copy-item-fromsession-cannot-be-found