none
Copy files to a remote server(s) as job.

    Question

  • Hi.

    To copy files from client to server machine as job I've tried script like this:

    $remote_session = New-PSSession -ComputerName server_1 -Credential user_name
    Start-Job -ScriptBlock {
    Get-ChildItem -Path c:\folder_src -Recurse |
    Copy-Item -Destination c:\folder_dst -ToSession $remote_session }

    But when I use receive-job cmdlet - there is an error, informing, that $remote_session variable is not found

    in the ScriptBlock of the Start-Job cmdlet.

    Here is the text of the error:

    Cannot validate argument on parameter 'ToSession'. The argument is null or empty. Provide an argument
    that is not null or empty, and then try the command again.
        + CategoryInfo          : InvalidData: (:) [Copy-Item], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.CopyItemC
       ommand
        + PSComputerName        : localhost

    The following version works in case I use receive-job cmdlet to provide the password:

    Start-Job -ScriptBlock {
    $remote_session = New-PSSession -ComputerName server_1 -Credential user_name
    Get-ChildItem -Path c:\folder_src -Recurse |
    Copy-Item -Destination c:\folder_dst -ToSession $remote_session 

    There might be something with the scope of $remote_session variable.

    Tried to make it global, same results.

    Is there any solution?

    • Edited by Alexound Tuesday, January 8, 2019 1:42 PM Typo
    Tuesday, January 8, 2019 1:41 PM

All replies

  • $sb = {
        $remote_session = New-PSSession -ComputerName server_1
        Get-ChildItem -Path c:\folder_src -Recurse |
        Copy-Item -Destination c:\folder_dst -ToSession $remote_session
    }
    Start-Job -ScriptBlock $sb -Credential username


    \_(ツ)_/

    Tuesday, January 8, 2019 1:49 PM
  • Thanks for the quick reply.

    The script didn't work, though. Credentials in the Start-Job are used for the local machine, not for the remote.

    Script produces same error, that $remote_session variable is empty.

    Tuesday, January 8, 2019 5:18 PM
  • If in a domain then use domain credentials which will be used for the remote connection by default.  There is no need to use remote credentials with New-PsSession when running under a domain account that has remoting permission.

    If in a workgroup then pass the credential object to the job  as an argument.

    Start-Job -ArgumentList $credentials …

    Or -

    $sb = {
        param($session)
        Copy-Item -Path c:\folder_src -Destination c:\folder_dst -Recurse -ToSession $session
    }
    $session = New-PSSession -Name MySession -ComputerName server_1
    Start-Job -ScriptBlock $sb -ArgumentList $session


    \_(ツ)_/

    Tuesday, January 8, 2019 5:53 PM
  • I have tried this before but I always get permissions issue.

    The safest way that I was able to do it is to use the C:\Temp folder on the server.

    Example:

    clear
    $servername = 'DESTSERVER'
    Copy-Item "c:\source_folder\file.txt" -Destination "\\$servername\C$\Temp\file.txt"



    • Edited by RATG Friday, January 11, 2019 4:28 AM changed the c: to c$
    Friday, January 11, 2019 2:21 AM
  • I have tried this before but I always get permissions issue.

    The safest way that I was able to do it is to use the C:\Temp folder on the server.

    Example:

    clear
    $servername = 'DESTSERVER'
    Copy-Item "c:\source_folder\file.txt" -Destination "\\$servername\C:\Temp\file.txt"


    Look at your code.  It will not work.  Remote systems do not have drive letters.


    \_(ツ)_/

    Friday, January 11, 2019 2:23 AM
  • I fixed it. C: should be C$.

    clear
    $servername = 'DESTSERVER'
    Copy-Item "c:\source_folder\file.txt" -Destination "\\$servername\C$\Temp\file.txt"

    • Proposed as answer by RATG Saturday, January 19, 2019 1:44 AM
    Friday, January 11, 2019 4:30 AM
  • I fixed it. C: should be C$.

    clear
    $servername = 'DESTSERVER'
    Copy-Item "c:\source_folder\file.txt" -Destination "\\$servername\C$\Temp\file.txt"

    Please stop adding nonsense to other users questions.  It is annoying and serves no helpful pupose the the user  who is asking a question.


    \_(ツ)_/

    Saturday, January 19, 2019 1:53 AM