Search code examples
powershellraspberry-piraspbian

How can I copy a whole folder to a certain IP adress with powershell?


I am currently trying to implement a functionality (preferably in powershell), which would regularily (every hour) copy a certain folder from my desktop computer to another computer.

The other computer is not listed in my local network, but I do have the IP, as well as the credentials of an user with the necessary username and password.

So far, I tried to use robocopy, but I can't quite seem to figure out how to get it to work.

If it matters: My desktop computer is running Windows 10, while the other computer is a raspberry pi running on Raspbian.

Is there any example code available of already successful attempts at doing this?


Solution

  • robocopy doesn't handle authentication. If you need to provide credentials for accessing a remote (SMB) share you need to map it to a drive letter before you can use it with robocopy. That is assuming your RasPi is running Samba.

    net use X: \\192.168.23.42\share /user:username password
    robocopy C:\some\folder X:\ /s
    net use X: /d
    

    The above code runs in CMD as well as in PowerShell.

    You can also map network drives via New-PSDrive, but that requires a bit more code:

    $pw   = ConvertTo-SecureString -String 'password' -AsPlainText -Force
    $cred = New-Object Management.Automation.PSCredential ('username', $pw)
    New-PSDrive -Name X -PSProvider FileSystem -Root '\\192.168.23.42\share' -Credential $cred -Persist | Out-Null
    robocopy C:\some\folder X:\ /s
    Remove-PSDrive -Name X
    

    If your credentials are not for SMB but for SSH access you could use the WinSCP .Net assembly.