eyeswideclosed
MIS
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?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
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