I am building a CICD pipeline using GitLab-CI.
The final stage of my pipeline transfers files from the build server (where the gitlab-runner.exe is installed) to the dev server. (Both build and dev servers are Windows Server 2019, by the way).
The gitlab-runner.exe uses the PowerShell executor, so each job is just a series of PowerShell commands to be run from the build server.
All the jobs work fine, except for the file transfer, which is a simple job like this:
I’ve set up OpenSSH Client and Server on the build and dev server respectively, and set up key-based authentication between them.
Manually, I can SCP and SSH from the build server to the dev server just fine and without password authentication required.
But when a simple SCP command in the job above is run by the gitlab-runner.exe in a pipeline, it just doesn’t complete. The pipeline does not fail, it just remains on that job until timeout (1hr).
I even tried putting the scp command into a powershell script located on the build server, and changing the job so that it just runs the script as below:
transfer_job
stage: transfer
script:
– C:scripts ransfer-script.ps1
This also fails, with the same problem. Timeout.
All I need is a reliable way to transfer files from server to server within a reasonable amount of time.
Any help would be extremely valuable.
About PowerShell
PowerShell is Microsoft’s task automation framework, consisting of a command-line shell and associated scripting language built on .NET.
Windows PowerShell (powershell.exe) is built into Windows 7 and newer; and is optionally available for Windows 98 SP2 and newer.1 It uses .NET Framework. Its successor, PowerShell (pwsh.exe), previously known as PowerShell Core, aka PowerShell 6/7, is cross-platform and can be optionally installed in Windows. It uses .NET (previously known as .NET Core).
PowerShell scripts can be directly executed, they do not need to be compiled first.
PowerShell Scripting
From WinSCP scripting perspective, an important aspect of PowerShell is its ability to run simple, yet powerful, scripts that can make use of functionality exposed by WinSCP .NET assembly.
powershell.exe -File upload.ps1
PowerShell (Core)’s pwsh.exe installs into C:Program FilesPowerShell.
or use -ExecutionPolicy argument for every script run:
powershell.exe -ExecutionPolicy Unrestricted -File upload.ps1
Installing the Assembly
First, you need to install the WinSCP .NET assembly. In most cases, all you need to do is download the WinSCP-X.X.X-Automation.zip package4 and extract it along with your PowerShell script.5
The version of WinSCPnet.dll in the root of the package is the .NET Framework build of the assembly. It can be used with Windows PowerShell only. With PowerShell (Core) 6/7, you have to use the .NET Standard build of the assembly, which is located in the netstandard2.0 subfolder.
For specific cases, read full instructions to installing the WinSCP .NET assembly.
Using from PowerShell
You use WinSCP .NET assembly from PowerShell as any other .NET assembly.
Loading Assembly
PowerShell script needs to load the assembly before it can use classes the assembly exposes. To load assembly use Add-Type cmdlet.6
Had you need to run the script from other directory, you need to specify a full path to the assembly. You can derive the path from the script file path using $PSScriptRoot automatic variable:7
-Path $PSScriptRoot
If you are writing a script that you plan to use as a WinSCP extension (a custom command), you can use the copy of the assembly installed with WinSCP. In that case you can use the WINSCP_PATH environment variable to resolve the path to the assembly. To allow the script run even outside of WinSCP, you should fall back to the $PSScriptRoot approach (as above), if the variable is not defined:
$assemblyPath = $env:WINSCP_PATH $env:WINSCP_PATH $PSScriptRoot
-Path $assemblyPath
The Session class exposes several events.
If you need to make use of these events:
PowerShell Module
There is a third-party PowerShell module, WinSCP PowerShell Wrapper, that provides a cmdlet interface on top of the .NET assembly.
# Set credentials to a PSCredential Object.
$credential =
# Create a WinSCP Session.
$session = -Hostname -Credential $credential -SshHostKeyFingerprint
# Using the WinSCPSession, download the file from the remote host to the local host.
-WinSCPSession $session -Path -Destination
# Remove the WinSCPSession after completion.
-WinSCPSession $session
Accomplish the same task with one line of code:
Example
This example is functionally equivalent to overall C# example for WinSCP .NET assembly.
There are also other PowerShell examples.
Converting Script to PowerShell Code
When you are considering converting your script to code that uses WinSCP .NET assembly, PowerShell, thanks to its ubiquity, can be a good choice, particularly, when you do not have your own preferred language that supports .NET.
Last modified: 2023-10-30 by martin
However, that changed when OpenSSH became available as an optional feature in Windows 10 and Windows Server 2019. Because Windows now natively offers both SSH server and SSH client, there is no reason to ever use telnet or FTP, which have no encryption capabilities — SSH offers superior security when you need to connect to a server or copy files over a network connection.
This article details how to install OpenSSH and use it to securely transfer files using either WinSCP or PowerShell.
What is OpenSSH used for?
These tasks are secure because OpenSSH includes the Secure Copy protocol (SCP) and the SSH File Transfer protocol (SFTP), and OpenSSH encrypts all traffic through a secure tunnel, enabling you to transmit sensitive data across the internet or unsecure network. The tunnel is created when the client and server exchange public keys.
How to Add the OpenSSH Feature on Windows
To install OpenSSH on a Windows machine:
Get-WindowsCapability -Online -Name “Open*”
You can see in the screenshot below that OpenSSH client is installed but OpenSSH server is not.
You can install both the OpenSSH server and the OpenSSH client using either Windows settings or PowerShell, as explained below.
Installing OpenSSH using Windows Settings
Alternatively, you can install the OpenSSH server and client components using Windows PowerShell.
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
Start-Service sshd
Set-Service -Name sshd -StartupType ‘Automatic’
Get-NetFirewallRule -Name *ssh*
The output below shows that a rule has already been created.
New-NetFirewallRule -Name sshd -DisplayName ‘OpenSSH Server (sshd)’ -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
Test-NetConnection -ComputerName -Port 22 -InformationLevel Detailed
If the test is successful, you will see something like this:
If it fails, then it could mean several things related to network connectivity; such as the Firewall rule not being created or enabled on a remote computer.
How to Move a File using SSH
Moving a file using SSH is nearly the same as moving one with an FTP application like WinSCP. Note that you must have permission to read the file on the source machine and write permission on the destination directory.
Moving a File using WinSCP
First, let’s first use the WinSCP utility to test out the new SSH server. (If needed, you can download WinSCP here.) Note that if you use WinSCP, you do not need to enable the OpenSSH client.
Copy a File or Directory using OpenSSH
If the directory does not exist on the target server, it will be created (assuming you have permission to do so).
You can also copy files back and forth from Linux and Unix servers to Windows servers and desktops.
How Netwrix Can Help
If you are moving or copying data files, chances are you need to protect that data — not just during the transfer operation but throughout its lifecycle. Data access governance software from Netwrix provides an effective and scalable approach to file activity monitoring. Moreover, it will help you reduce the risk of cybersecurity incidents by enabling you to understand who has access to what and strictly limit access to sensitive data. You can:
Frequently Asked Questions
How do you move a file using SSH?
When working with Unix-like systems, you can use the mv command to move a file from one location to another on a remote server using SSH.
For Windows machines, you can either install a third-party SSH server and client application, or enable the OpenSSH feature (requires Windows Server 2019 or 2022, or Windows 10 or 11).
In either case, you must have the logon credentials to connect, as well as the necessary permissions to read the file on the source machine and write permission on the destination directory.
Does SSH allow file transfer?
Yes. SSH includes the Secure Copy protocol (SCP) and SSH File Transfer protocol (SFTP), which enable the secure transfer of files between a local host and a remote host.
How can I transfer files from an SSH server to a local machine?
Alternatively, you can use third-party SSH utilities that offer easy-to-use menus.
Ethan is a Product Manager at Netwrix. With over 10 years of experience in the software industry, he is responsible for building and delivering on the roadmap for the Netwrix Enterprise Auditor product.