use powershell to upload a file scp
This Blog will show detailed information almost SCP, Installation of SCP on Powershell (Windows host), applied demos to use SCP by transferring data between windows Powershell and a Linux host, etc.
What is SCP?
SCP for Secure Copy Protocol. SCP can be used to deeply transfer files betwixt local and remote hosts. We can consider it similar to a secured FTP. By default, SCP runs on Port 22. The SCP client can upload files to the SSH server or asking files and folders for downloading.
Almost the module: Posh-SSH
It is a windows Powershell module that uses a custom version of the SSH.NET Library to provide SSH functionality in windows Powershell. It provides functionality for automating SSH, SFTP and SCP actions. It supports public key, password and keyboard-interactive methods for authentication. Information technology supports RSA and DSA private central machinery. We tin besides use SOCKS4, SOCKS5 and HTTP Proxy methods. Remote, dynamic and local port forwarding can exist done by SCP.
The chief purpose of the module is to facilitate automating deportment confronting one or multiple SSH enabled servers. This module is more than useful to ruby teamers to transfer the data between hosts using Powershell.
Note: This module is for Windows PowerShell 3.0 or above. It is compiled for .NET Framework iv.5.
By default, SCP Functionality is not installed as Powershell cmdlets.
Now to utilize it, we have two options:
i. Install the SCP module by the post-obit command :
Install-Module -Name Posh-SSH OR
ii. Download the module from Github repository and include it at runtime directly in Powershell, Then utilize it direct.
Here nosotros gonna use this method. So let'due south start.
Download Posh-SSH grade Github Repository: https://github.com/darkoperator/Posh-SSH
Navigate Powershell to the directory : \Posh-SSH-master\Release\
Import Posh-SSH module by using the post-obit control:
Import-Module .\Posh-SSH.psd1
Subsequently including the module, we tin use all the functionality provided past Posh-SSH :
- Get-SCPFile – Download files from remote servers.
- Get-SCPFolder – Download the Entire binder from remote server.
- Get-SCPItem – Download both files or folders from the remote servers.
- Set-SCPFile – Upload files into remote servers.
- Ready-SCPFolder – Upload the Entire folder into the remote server.
- Set-SCPItem – Can upload both files or folders to the remote servers.
And many more included Documentation of the repository.
Before using this module on Powershell, let'southward confirm that SSH service is running on remote host. Use command service ssh status to ostend.
If SSH is not running and then start the SSH service on the remote host using the command :
service ssh first At present to utilize the SCP Functionality with the Posh-SSH module, nosotros need to supply username and password as PSCredential to Powershell for authentication of the remote host. Run the post-obit command :
$credential = Go-Credential
Supply the credentials of the remote host and click OK. It will store credentials in a Powershell variable that nosotros can utilize farther directly.
Download files from remote server Using Get-SCPFile :
Employ the post-obit control:
Get-SCPFile -ComputerName 'REMOTE_SERVER_IP' -Credential $credential -RemoteFile "REMOTE_SERVER_FILE_PATH" -LocalFile "LOCAL_FILE_PATH"
Hither,
- ComputerName – Remote Host IP address
- Credential – Credentials variable
- RemoteFile – Remote file path which we desire to download
- LocalFile – Local file path and proper name to store the downloaded file on the local car.
Download Folder from remote server Using Go-SCPFolder :
Use the following command:
Become-SCPFOlder -ComputerName 'REMOTE_SERVER_IP' -Credential $credential -RemoteFolder "REMOTE_SERVER_DIR_PATH" -LocalFolder 'LOCAL_DIR_PATH'
Here,
- ComputerName – Remote Host IP address
- Credential – Credentials variable
- RemoteFile – Remote directory path which we want to download
- LocalFile – Local directory path and name to store downloaded content of a directory on the local automobile.
Here we can also use Go-SCPItem module. Go-SCPItem module tin can be used to download anything from the remote server, information technology can exist files or directory. It is a general-purpose module to download the data from the server.
Command :
Get-SCPItem -ComputerName 'REMOTE_SERVER_IP' -Credential $credential -Destination "REMOTE_SERVER_FILE_PATH" -Path 'LOCAL_DIR_PATH' Upload File to the remote server:
The file tin be uploaded on the remote host using 2 SCP methods
- Set-SCPFile
- Ready-SCPItem
Upload file by Set-SCPFile :
Use the following command:
Set-SCPFile -ComputerName 'REMOTE_SERVER_IP' -Credential $credential -RemotePath "REMOTE_SERVER_DIR_PATH" -LocalFile 'LOCAL_FILE_PATH'
Here,
- ComputerName – Remote Host IP address
- Credential – Credentials variable
- RemotePath – Remote directory path where we want to upload the file.
- LocalFile – Local file path and name which is to be uploaded on the server.
At present, We can confirm on Linux host that the file is uploaded successfully,
Upload file by Ready-SCPItem :
Use the post-obit command:
Set up-SCPItem -ComputerName 'REMOTE_SERVER_IP' -Credential $credential -Destination "REMOTE_SERVER_FILE_PATH" -Path 'LOCAL_FILE_PATH'
Here,
- ComputerName – Remote Host IP accost
- Credential – Credentials variable
- Destination – Remote directory path where we want to upload the file.
- Path – Local file path and name which is to be uploaded on the server.
Confirm on Linux host that the file is uploaded successfully,
Upload Binder to remote server Using Set up-SCPItem :
Set-SCPItem module can be used to upload both files and folders on remote server. It is very useful to transfer content considering it is a full general-purpose module.
Use the following control:
Set-SCPItem -ComputerName 'REMOTE_SERVER_IP' -Credential $credential -Destination "REMOTE_SERVER_DIR_PATH" -Path 'LOCAL_DIR_PATH\*'
Here,
- ComputerName – Remote Host IP accost
- Credential – Credentials variable
- Destination – Remote directory path where we want to upload the folder.
- Path – Local directory path and proper noun which is to be uploaded on the server. Here, don't forget to use "\*" at the finish otherwise, information technology will generate an mistake.
Confirm on Linux host that the directory content is uploaded successfully,
And then, here the content of local binder "evil" is copied to the remote folder "/home/jj/examination/".
Annotation: To upload folders on remote host some other command "Ready-SCPFolder" tin exist used, merely it will generate the following mistake when I am trying to utilize information technology.
Error: Ready-SCPFolder : scp: error: unexpected filename: .
So, Set-SCPItem is the best module to copy contents to remote hosts because It tin upload both files and folders.
There are many more modules of SCP available in Posh-SSH library. We can do many things using this. And so become through all the modules included once, to make wider your knowledge.
Reference:
- Documentation of Posh-SSH modules: https://github.com/darkoperator/Posh-SSH/tree/master/docs
Source: https://thedarksource.com/powershell-scp-to-transfer-files-between-windows-linux/
0 Response to "use powershell to upload a file scp"
Post a Comment