Session.ExecuteCommand Method

Executes command on the remote server.

Syntax

C#
public CommandExecutionResult ExecuteCommand(string command)
VB.NET
Public Function ExecuteCommand(ByVal command As String) As CommandExecutionResult

Parameters

Name Description
string command Command to execute.

Return Value

CommandExecutionResult with command output. See also Capturing results of operations.

Advertisements:

Exceptions

Exception Condition
InvalidOperationException Session is not opened.
SessionLocalException Error communicating with winscp.com.
See the exception documentation for details.
SessionRemoteException Session has failed.
See the exception documentation for details.
TimeoutException Timeout waiting for winscp.com to respond.

Remarks

With SFTP and SCP protocols, executes arbitrary remote shell command. With FTP protocol, executes a protocol command.

With SFTP protocol, that does not allow execution of arbitrary remote command, separate shell session will be automatically opened.

Not supported with WebDAV protocol.

The command must not require user input.

Example

C# Example

using System;
using WinSCP;
 
class Example
{
    public static int Main()
    {
        try
        {
            // Setup session options
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Sftp,
                HostName = "example.com",
                UserName = "user",
                Password = "mypassword",
                SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
            };
 
            using (Session session = new Session())
            {
                // Connect
                session.Open(sessionOptions);
 
                // Execute mysqldump on the server to dump all MySQL databases and compress the results
                const string dbUsername = "USERNAME";
                const string dbPassword = "PASSWORD";
                const string tempFilePath = "/tmp/all_databases.gz";
 
                string dumpCommand =
                    string.Format("mysqldump --opt -u {0} --password={1} --all-databases | gzip > {2}",
                                  dbUsername, dbPassword, tempFilePath);
                session.ExecuteCommand(dumpCommand).Check();
 
                // Download the database dump
                session.GetFiles(tempFilePath, "D:\\dbbackup\\").Check();
            }
 
            return 0;
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: {0}", e);
            return 1;
        }
    }
}

VB.NET Example

Imports WinSCP
 
Friend Class Example
 
    Public Shared Function Main() As Integer
 
        Try 
            ' Setup session options
            Dim sessionOptions As New SessionOptions
            With sessionOptions 
                .Protocol = Protocol.Sftp
                .HostName = "example.com"
                .UserName = "user"
                .Password = "mypassword"
                .SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
            End With
 
            Using session As New Session
                ' Connect
                session.Open(sessionOptions)
 
                ' Execute mysqldump on the server to dump all MySQL databases and compress the results
                Const dbUsername As String = "USERNAME"
                Const dbPassword As String = "PASSWORD"
                Const tempFilePath As String = "/tmp/all_databases.gz"
 
                Dim dumpCommand As String = _
                    String.Format("mysqldump --opt -u {0} --password={1} --all-databases | gzip > {2}", _
                        dbUsername, dbPassword, tempFilePath)
                session.ExecuteCommand(dumpCommand).Check()
 
                ' Download the database dump
                session.GetFiles(tempFilePath, "D:\dbbackup\").Check()
            End Using
 
            Return 0
        Catch e As Exception
            Console.WriteLine("Error: {0}", e)
            Return 1
        End Try
 
    End Function
 
End Class

PowerShell Example

Learn more about using WinSCP .NET assembly from PowerShell.

try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "WinSCPnet.dll"
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "example.com"
        UserName = "user"
        Password = "mypassword"
        SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
    }
 
    $session = New-Object WinSCP.Session
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
 
        # Execute mysqldump on the server to dump all MySQL databases and compress the results
        $dbUsername = "USERNAME"
        $dbPassword = "PASSWORD"
        $tempFilePath = "/tmp/all_databases.gz"
 
        $dumpCommand =
            ("mysqldump --opt -u {0} --password={1} --all-databases | gzip > {2}" -f
                $dbUsername, $dbPassword, $tempFilePath)
        $session.ExecuteCommand($dumpCommand).Check()
 
        # Download the database dump
        $session.GetFiles($tempFilePath, "D:\dbbackup\").Check()
 
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch [Exception]
{
    Write-Host ("Error: {0}" -f $_.Exception.Message)
    exit 1
}
 
  library_session_executecommand.txt · Last modified: by martin
 

Search Documentation

This page

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

Associations

Site design by Black Gate