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

285 views Asked by At

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?

2

There are 2 answers

0
Martin Prikryl On BEST ANSWER

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:

2
KESHAV BORATE On

Go to your WinSCP profile (Session > Sites > Site Manager)

Click on Edit > Advanced... > Environment > SFTP

Insert sudo su -c /usr/lib/sftp-server in "SFTP Server" (note this path might be different in your system)

Save and connect