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

Issue

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:

Answered By – Martin Prikryl

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published