开发者

Run script on machine from other machine in C#

开发者 https://www.devze.com 2023-03-23 05:16 出处:网络
I\'m writing a Web Service (WCF) for my work and I\'m looking for a way to run script on demand on other machine.

I'm writing a Web Service (WCF) for my work and I'm looking for a way to run script on demand on other machine.

We got machines that we connect from RDC, and I want to run a sc开发者_StackOverflow中文版ript on it from another C# program.

Also, I can't seem to find a way to run an executable file on another machine from C#.


The reason why you can't find a part of the .Net framework that lets you run executables on another machine is because there isn't one.

If you want a straightfoward way of running an executable on a remote machine then you may be interested in PsExec (a Sysinternals tool released by Micrososft).


this is possible using WMI via C# (see http://www.codeproject.com/KB/cs/Remote_Process_using_WMI_.aspx) or via commandline using http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx ... but it is something you usually should NOT do - it creates several security issues to deal with...

EDIT - WMI with User/PW:
connectionoptions gives you the possibility to supply a UserName + Password - see http://msdn.microsoft.com/en-us/library/system.management.connectionoptions.aspx


I know this is an old post, but I would like to add that it is certainly possible to run a remote executable without PsExec which many virus software flags as problematic. Also most sys admins don't want PsExec on their web servers. And, running a remote executable via mapped drive or UNC does not mean that you have the license installed, so it may fail (or run a demo version) depending on the software.

The key is to wrap the System.Diagnostics.Process steps in a WCF service. Here is a partial example...

{
// Define a service contract.
[ServiceContract(Namespace = "http://MyDataServices.foo.com")]
public interface IDataService
{
    [OperationContract]
    bool ProcessStatTransfer(MemoryStream inputCommands, string inputName);

}

// Implement the IDataService service contract in a service class.
public class DataService : IDataService
{

    // Implement the IDataService methods.
    public bool ProcessStatTransfer(MemoryStream inputCommands, string inputName)
    {
        try
        {
            // StatTransferLocation is C:\Program Files\StatTransfer12-64\st.exe
            // on the machine that hosts the service
            string m_stattransfer_loc = ConfigurationManager.AppSettings["StatTransferLocation"].ToString();
            string m_stattransfer_file = ConfigurationManager.AppSettings["CommandFiles"].ToString() + inputName;

            using (FileStream m_fsfile = new FileStream(m_stattransfer_file, FileMode.Create, FileAccess.Write))
            {
                inputCommands.WriteTo(m_fsfile);
            }

            ProcessStartInfo processInfo = new ProcessStartInfo("\"" + m_stattransfer_loc + "\"");

            processInfo.Arguments = "\"" + m_stattransfer_file + "\"";
            processInfo.UseShellExecute = false;
            processInfo.ErrorDialog = false;
            processInfo.CreateNoWindow = true;

            Process batchProcess = new Process();
            batchProcess.StartInfo = processInfo;
            batchProcess.Start();

            return true;

        }
        catch 
        {

            return false;

        }

    }
.....

Then you add a service reference and invoke the method. No mappings, PsExec, or WMI. It is a pure C# solution.

0

精彩评论

暂无评论...
验证码 换一张
取 消