[SOLVED] - Copy-item Error remote localhost

Hello,
I have a problem when I want to copy a file from one server to another.
The copy of the data is done with an authentication and when I run the script, I get the following error message:
Connection to the remote server localhost failed with the following error message: Access denied
I have run the command “Enable-PSRemoting -Force” but it does not work.
Here is my script

$username = "username"
$password = "password"
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
$Session = New-PSSession -Credential $cred
$remote_share="\\srv\remote_share"
$local_share="F:\tshare"
copy-item $local_share $remote_share -Fromsession $session -recurse -force

Can someone please help me?

Axel,
Welcome to the forum. :wave:t4:

Is this supposed to be an automatically running task or do you want to run it manually?

Regardless of that …

Do you want to copy the item from the computer you run this script on TO another computer, right? Then you should use the parameter -ToSession instead of -FromSession.

If you use the parameter -FromSession or -ToSession you use a local path for both parameters … -Path AND -Destination.

If this is supposed to be an automatically running task I’d recommend using robocopy and set the task to be run with the context of the user with access to the target folder. THat’s the easiest and probably most robust way.
Another simply and very straight forward way would be to grant access to the user you run this script with.

Hello Olaf,
Thanks for your welcome
but in fact, I found out where my problem came from. I forgot to tell my user that he had the possibility to execute the script.
To do so, you have to execute the command:

Set-PSSessionConfiguration -ShowSecurityDescriptorUI -Name Microsoft.PowerShell