Home > Mobile >  A parameter cannot be found that matches parameter name 'FromSession'
A parameter cannot be found that matches parameter name 'FromSession'

Time:02-01

I am trying to copy some files from remote servers to local with the following:

function getfiles {

& C:\Users\myuser\Desktop\stores\getfiles.ps1
  }

function create {
New-Item -ItemType directory -Force -Path C:\Users\myuser\Desktop\stores
}
#####copy files to local#####

$computer = Get-Content C:\Users\myuser\Desktop\stores\computers.txt
$computer | ForEach-Object {
$session=new-pssession -computername $_ -credential (Import-Clixml "mycredentials.xml")
Invoke-Command -Session $session -ScriptBlock ${function:create}
Copy-Item -path "C:\Users\myuser\Desktop\stores\getfiles.ps1" -destination "C:\Users\myuser\Desktop\stores\getfiles.ps1" -recurse -ToSession $session 
Invoke-Command -Session $session -ScriptBlock ${function:getfiles}
Copy-Item -path "C:\Users\myuser\Desktop\stores\UK*" -Destination "C:\Users\myuser\Desktop\stores" -recurse -FromSession $session
Get-PSSession -ComputerName $_ -Credential (Import-Clixml "mycredentials.xml") | Disconnect-PSSession | Remove-PSSession
}

For some strange reason the

Copy-Item -path "C:\Users\myuser\Desktop\stores\UK*" -Destination "C:\Users\myuser\Desktop\stores" -recurse -FromSession $session

Stopped working with the following error:

Copy-Item : A parameter cannot be found that matches parameter name 'FromSession'.
At line:20 char:122
  ... "C:\Users\myuser\Desktop\stores" -recurse -FromSession $sessio ...
                                                       ~~~~~~~~~~~~
      CategoryInfo          : InvalidArgument: (:) [Copy-Item], ParameterBindingException
      FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand

Some notes:

  1. The drives on both servers exist, not the bug mentioned in couple of topics.
  2. The getfiles.ps1 executes as expected and it is for file creation.

CodePudding user response:

As mentioned in the comments, this is a long-standing bug that was recently fixed.

Basically, whenever the -Path argument contains wildcard expressions (eg. *) that fail to resolve to any local file system items, the runtime will skip generation of dynamic parameters for the target provider (in this case the FileSystem provider).

-ToSession and -FromSession happens to be dynamic provider parameters.

As you've already found, moving the wildcard to the -Filter parameter works:

Copy-Item -Path "C:\Users\myuser\Desktop\stores" -Filter 'UK*' -Destination "C:\Users\myuser\Desktop\stores" -Recurse -FromSession $session
  •  Tags:  
  • Related