Run bat file on remote machine

C

Ctal

What is the best way to have my windows app run a .bat file on a remote
server? The .bat will need to run as an admin on the remote machine.
Additonally I need for my windows app to wait for the .bat to finish.
 
A

Alvin Bruney [MVP]

have a look at the process names pace assuming you have a share or some way
to access the remote machine, this should work.

--
________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
 
W

Willy Denoyette [MVP]

| What is the best way to have my windows app run a .bat file on a remote
| server? The .bat will need to run as an admin on the remote machine.
| Additonally I need for my windows app to wait for the .bat to finish.
|

There is nothing in .NET that makes this possible, even WMI is not able to
start a process as a local admnistrator on a remote system.
Take a look at psexec at
h<ttp://www.sysinternals.com/Utilities/PsExec.html>, this utility can run a
process on a remote system with specified credentials, from your code you
can start psexec using Process.Start.

Willy.
 
W

Willy Denoyette [MVP]

"Alvin Bruney [MVP]" <www.lulu.com/owc> wrote in message
| have a look at the process names pace assuming you have a share or some
way
| to access the remote machine, this should work.
|

Alvin, The OP's requirement is to run a batch file remotely, this is not
possible using this namespace. You need a client/server style application
like Sysinternals - www.sysinternals.com psexec.exe to run a process
remotely.


Willy.
 
A

Alvin Bruney [MVP]

I'm not sure what you mean here, the process class can start a local or
remote process. If you mean the bat file, then you are right, it won't start
it but that can be fixed easily by writing a small exe to either call the
bat file or reproduce the bat file functionality in the exe.

--
________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
 
W

Willy Denoyette [MVP]

"Alvin Bruney [MVP]" <www.lulu.com/owc> wrote in message
| I'm not sure what you mean here, the process class can start a local or
| remote process. If you mean the bat file, then you are right, it won't
start
| it but that can be fixed easily by writing a small exe to either call the
| bat file or reproduce the bat file functionality in the exe.
|
| --
|

While the Process class can be used to 'monitor' processes on remote
machines, it can't start (that is run a process remotely) or stop a process
on a remote machine. The Process class call's CreateProcess or
CreateProcessWithLogon, and these API's can only create local processes
(Windows has no API to create processes remotely).
If you have to run a process remotely, you have to resort to WMI, or another
C/S (like Psxec.exe) kind of architecture, here the 'client' requests the
'server' to start a process on a remote server (where it gets created as a
local process).


Willy.
 
C

Ctal

Willy said:
| What is the best way to have my windows app run a .bat file on a remote
| server? The .bat will need to run as an admin on the remote machine.
| Additonally I need for my windows app to wait for the .bat to finish.
|

There is nothing in .NET that makes this possible, even WMI is not able to
start a process as a local admnistrator on a remote system.
Take a look at psexec at
h<ttp://www.sysinternals.com/Utilities/PsExec.html>, this utility can run a
process on a remote system with specified credentials, from your code you
can start psexec using Process.Start.

Willy.

Thanks Willy, that looks promising. The other way I was approaching
this was with ado commands, my remote machine has sql server installed.
I was going to call xp_cmdshell and use that to launch the .bat (or
exe). That works if the SQL service account has the necessary
credentials (in my case it does)

Thanks all for the suggestions.
 
A

Alvin Bruney [MVP]

You are right Willy. I tried unsuccessfully to do this but it would only
fail silently. I swore I did this a couple years ago but when I went back to
check the code, it wasn't a actually a mapped drive, it was
d:\somepath\somefile.

--
________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top