Search code examples
linuxpowershellwinscpsusewinscp-net

Run script on remote SUSE Linux with root privilege using WinSCP .NET assembly


I'm trying to execute sh script from Windows on Remote SUSE Linux using WinSCP .NET assembly.

I've created a session as follow:

$sessionOptions = New-Object WinSCP.SessionOptions -Property @
{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "RemoteIp"
    UserName = "username"
    Password = "password"
}
$session = New-Object WinSCP.Session

I run the sh script

$session.ExecuteCommand("bash /home/script.sh" )

and I get permissions errors, for example:

rm: cannot remove '/somefolder': Permission denied.

Simple command like uname works fine.

Any idea how can I log in as root?


Solution

  • You have to run your command through sudo:

    $session.ExecuteCommand("sudo bash /home/script.sh")
    

    Though WinSCP does not support providing input to commands. So your remote system must be configured not to require sudo password (at all, or for that specific command).

    Some references: