Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run an exe on another machine?

Status
Not open for further replies.
Jun 24, 2005
340
US
I need to call an exe on server A from server B and have it run on A. This sounds easy but I have found no way to do it, the exe loads on the server that calls it no matter what I try. Ideas?
 
Chiph may have some good remoting or cross domain way of doing this, but the first thought that popped into my head would be to run a service on the target machine that listens for a network stream to send it a command. Then the service on the target machine can launch the program specified by the command. You would change your local app to connect to the target machine's server and then pass the command to it.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Here is a class that I use to execute a program on another server and wait for it to finish, using WMI. I found the code on the Internet and then I added the part to wait for it to finish. Unfortunately, I cannot find the original web site that I got it from, or I would give credit where it is due.
Code:
Imports System.Management

Public Class RemoteProcess

    Private Shared pids As New ArrayList

    Public Shared Sub Execute(ByVal cmd As String, ByVal machine As String, ByVal timeoutSeconds As Integer)
        'Execute a command (cmd) on a remote machine (machine), and wait a 
        'certain amount of time for it to finish (timeoutSeconds). Do not use 
        'this method if the command may finish to fast because the process_end 
        'event won't fire.
        Dim wait As Boolean = timeoutSeconds > 0

        ManagementPath.DefaultPath = New ManagementPath(String.Format("\\{0}\root\CIMV2", machine))
        Dim procStart As New ManagementClass("Win32_ProcessStartup")
        Dim ps As ManagementObject = procStart.CreateInstance()
        ps("ShowWindow") = 1

        Dim procClass As New ManagementClass("Win32_Process")
        SetOptions(procClass)

        Dim watcher As New ManagementEventWatcher("select * from __instancedeletionevent WITHIN 0.1 where Targetinstance ISA 'Win32_Process'")
        AddHandler watcher.EventArrived, AddressOf ProcessEnd
        pids.Clear()
        watcher.Start()

        Try
            Dim p() As Object = {cmd, Nothing, ps, Nothing}
            procClass.InvokeMethod("Create", p)
            'Wait until the process created no longer exists on the remote machine
            If wait Then
                Dim pid As Integer = Convert.ToInt32(p(3))
                Dim endTime As DateTime = DateTime.Now.AddSeconds(timeoutSeconds)
                While DateTime.Compare(DateTime.Now, endTime) < 0 AndAlso Not pids.Contains(pid)
                    System.Threading.Thread.Sleep(1000)
                End While
                If Not pids.Contains(pid) Then
                    Throw New ApplicationException("Process timed out")
                End If
            Else
                System.Threading.Thread.Sleep(500)
            End If
        Finally
            watcher.Stop()
        End Try

    End Sub

    Private Shared Sub ProcessEnd(ByVal sender As Object, ByVal e As EventArrivedEventArgs)
        'The event handler for the Process_End WMI event
        Dim pid As Object = CType(e.NewEvent.Properties("TargetInstance").Value, ManagementBaseObject).Properties("Handle").Value
        pids.Add(Convert.ToInt32(pid))

    End Sub

    Private Shared Sub SetOptions(ByVal mo As ManagementObject)
        'Set the authentication/impersonation options this ManagementObject will use.
        mo.Scope.Options.Authentication = AuthenticationLevel.Default
        mo.Scope.Options.Impersonation = ImpersonationLevel.Impersonate
        mo.Scope.Options.EnablePrivileges = True
    End Sub

End Class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top