Shashi Ranjan
Shashi Ranjan

Reputation: 1561

run command as administrator on remote windows machine

Situation: Running the bat file on windows machine:
1. When I double click the bat file: Bat running is failed.
2. When I right click on bat file and run as administrator: Bat run is successful.

Now I have to run this bat file successfully from remote machine.

What I did:
1. Installed freeSSDd on remote machine and configured administrator user on freeSSHd to access shell and SFTP.
2. Now I am able to login to the remote machine using putty.

Problem:
I am not able to run the bat file successfully. How can I achieve this?

I also used runas /savecred /user:administrator C:/install.bat, but It didn't helped.

Upvotes: 0

Views: 12258

Answers (2)

John Rigler
John Rigler

Reputation: 303

If you have installed an ssh daemon, then you can run your BAT in a remote shell, but you remote shell may open up in something other than CMD.COM. I use cygwin to set up sshd and then from a remote machine, if I ssh in to run a command, it is using cygwin's bash. I can run a BAT file, but need to call CMD first:

ssh WINDOWS_SERVER "cmd /C D:\PATH_TO_BAT\BATCHFILE.BAT"

But there are some pieces missing here. I looked briefly at the Freesshd page and saw only graphical interfaces. Does freesshd support remote command execution, or just secure fire transfer? And what sort of shell get executed on the windows server when you run it?

cygwin is an entire Linux subsystem that runs under Windows and includes an sshd server, but might be a bit much for someone starting out: https://cygwin.com/

\n makes a powershell remote server that listens on port 22 (ssh) and dumps you into a powershell prompt, you can then use my steps above to call CMD from powershell, versus a bash shell.

http://www.powershellserver.com/

Upvotes: 0

MichaelS
MichaelS

Reputation: 6042

There is a way to get this working without any 3rd party software.

You have to create a task on the remote machine using the windows task scheduler which simply executes the desired command. There is an option where you can tell the scheruler to run a bat with a specific account. Enter an admin account and the password and check the "run with highest privileges" box. Leave "Triggers" empty, go to "Settings" and check the "Allow task to be run on demand" box. That's it!

Now when you want to run your file from a different location do

SCHTASKS /RUN /S <RemoteServerName> /U username /P password /TN "<task name>"

If you don't want to enter username and password each time you can adept the user policy (e.g. add the calling machine to the trusted list of the server).

Upvotes: 2

Related Questions