I need to write a script that transfers files from a folder onto another server (Linux), but the script that's transferring files is on windows, and I was wondering if there was an alternative to scp
for PowerShell (or if there was another way of doing this)
By default, the PowerShell Server does not allow SCP connections. This is easily enabled in the server interface using the following steps: On the Connection tab simply check the box that says “Enable Secure Copy Protocol (SCP) Support”. Then click Save Changes and Restart to restart the server with this change.
SFTP is widely considered the successor of SCP. It still runs on top of SSH for transport-layer security and doesn't require setting up access separately. It can give you a custom interactive prompt for exploring the remote filesystem or you can script with a pre-written series of commands.
Windows operating systemsMicrosoft Windows does not include an SCP client, so you must download one first. A2 Hosting recommends PSCP, a free program that you can download here. PSCP is part of the PuTTY tool suite for Windows.
You can run SCP on Windows via PuTTY. Use the SCP command via the Windows command line interface. You should start each command line with pscp -scp.
There is a handy little tool that comes with Putty called pscp.exe
that will do this and can be called in powershell easily.
Example below copies from windows to a CentOS box (logging in as the usercode "bill") and you use the -pw
switch in pscp
to pass in a password (otherwise the command window that is spawned will prompt for the Linux password):
Start-Process 'C:\Program Files (x86)\PuTTY\pscp.exe' -ArgumentList ("-scp -pw password C:\Document.rtf [email protected]:/home/bill/")
PuTTY Secure Copy client
Release 0.62
Usage: pscp [options] [user@]host:source target
pscp [options] source [source...] [user@]host:target
pscp [options] -ls [user@]host:filespec
Options:
-V print version information and exit
-pgpfp print PGP key fingerprints and exit
-p preserve file attributes
-q quiet, don't show statistics
-r copy directories recursively
-v show verbose messages
-load sessname Load settings from saved session
-P port connect to specified port
-l user connect with specified username
-pw passw login with specified password
-1 -2 force use of particular SSH protocol version
-4 -6 force use of IPv4 or IPv6
-C enable compression
-i key private key file for authentication
-noagent disable use of Pageant
-agent enable use of Pageant
-batch disable all interactive prompts
-unsafe allow server-side wildcards (DANGEROUS)
-sftp force use of SFTP protocol
-scp force use of SCP protocol
pscp.exe
is a viable option, but I have been using a library from Rebex for a couple years now for SFTP and FTPS transfers in both C# apps and PowerShell scripts with great success. Their package also includes an SCP object but I haven't personally used it.
It does cost money vs. pscp
being free. Before selecting the Rebex package, I had considered going the PuTTY route but my team decided that having a library we could easily roll into any app/script was worthwhile in the long term.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With