473,396 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Remote Process Execution in vb.net

Dll written in VB.NET 2003 to start a command remotely.

You can :
- launch the command and wait until it's finished. (Ex. 1)
- launch the command providing a timeout in seconds, it will wait until
it's finished. If the command didn't terminate within the timeout, the
command is killed. (Ex. 2)
- launch the command and continue your processing after the command has
been started remotely (Ex. 3).

*******************************
* Example code To use the dll *
*******************************

Dim RE As New remoteExec.remoteExec

MsgBox("0")
Try
RE.Execute("c:\windows\notepad.exe", "remoteSrv") 'Ex. 1 : wait
until the process is finished
Catch ex As Exception
MsgBox(ex.Message())
End Try
MsgBox("end 0")

MsgBox("10")
Try
RE.Execute("c:\windows\notepad.exe", "remoteSrv", 10) 'Ex. 2 :
timeout of 10 seconds
Catch ex As Exception
MsgBox(ex.Message())
End Try
MsgBox("end 10")

MsgBox("-1")
Try
RE.Execute("c:\windows\notepad.exe", "remoteSrv", -1) 'Ex. 3 :
starts the command, don't wait until it's finished
Catch ex As Exception
MsgBox(ex.Message())
End Try
MsgBox("end -1")

**********************
* Dll code in VB.NET *
**********************

Imports System
Imports System.Management

Public Class remoteExec

' We use the pids ArrayList to keep track of which processes have
ended
' on the remote machine.
Private Shared pids As ArrayList = New ArrayList

' Execute a certain command on a remote machine without
' waiting for it to finish.
Public Overloads Shared Sub Execute(ByVal cmd As String, ByVal
machine As String)
Execute(cmd, machine, 0)
' Passing 0 is useful for running things like "md c:\foo",
which end too quickly and for the Win32_Processtermination event to be
caught
End Sub

' Execute a certain command on a remote machine, and wait
' a certain amount of time for it to finish. Do not use this method
if your command line may
' finish to fast (e.g. "md c:\mydir") because the process_end event
won't fire.
Public Overloads Shared Sub Execute(ByVal cmd As String, ByVal
machine As String, ByVal timeoutSeconds As Integer)
'Dim wait As Boolean = (timeoutSeconds > 0)
ManagementPath.DefaultPath = New
ManagementPath(String.Format("\\{0}\root\CIMV2", machine))
Dim procStart As ManagementClass = New
ManagementClass("Win32_ProcessStartup")
Dim ps As ManagementObject = procStart.CreateInstance
ps("ShowWindow") = 1
Dim procClass As ManagementClass = New
ManagementClass("Win32_Process")
SetOptions(procClass)
Dim watcher As ManagementEventWatcher = 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 = New Object() {cmd, Nothing, ps,
Nothing}
procClass.InvokeMethod("Create", p)
Dim pid As Integer = Convert.ToInt32(p(3))
' We'll wait until the process created no longer exists on
the remote
' machine
If timeoutSeconds > 0 Then

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
'Kill the process
Dim myManagementObjectSearcher As New
ManagementObjectSearcher("Select * from Win32_Process Where ProcessID =
" + Convert.ToString(pid))
Dim myManagementObjectCollection =
myManagementObjectSearcher.Get
Dim observer As ManagementOperationObserver = New
ManagementOperationObserver
For Each myManagementObject As ManagementObject In
myManagementObjectCollection
myManagementObject.InvokeMethod(observer,
"Terminate", Nothing)
Next

Throw New ApplicationException("Process timed out -
killed PID = " + pid.ToString)
End If
ElseIf timeoutSeconds = 0 Then
' Wait until the command finish on the remote server

While Not pids.Contains(pid)
System.Threading.Thread.Sleep(1000)
End While
Else
' The command has been started. Wait 500 milliseconds
and leave the command running without control
System.Threading.Thread.Sleep(500)
End If
Finally
watcher.Stop()
End Try
End Sub

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

' Set the authentication/impersonation options this
ManagementObject will use.
Private Shared Sub SetOptions(ByVal mo As ManagementObject)
mo.Scope.Options.Authentication = AuthenticationLevel.Default
mo.Scope.Options.Impersonation = ImpersonationLevel.Impersonate
mo.Scope.Options.EnablePrivileges = True
End Sub
End Class
*************************
* Based on this C# code *
*************************
http://groups.google.fr/group/micros...5247d6a94159e3
(Carlos Garcia Jurado Suarez [MSFT])

http://www.expresscomputeronline.com...hspace02.shtml
(Yashavant Kanetkar)

Patrick.

Nov 21 '05 #1
0 13715

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: lists | last post by:
I would appreciate it you folks would answer a couple of basic questions about the "Remote execution" error: 1. Is it a browser issue. 2. Is it a PHP version issue. 3. Is a a $HTTP_POST_VAR...
5
by: Roger | last post by:
I would like to get a list of running processes on a remote machine. How is this possible via VB.Net? Is it possible? Can someone point me in the right direction. thanks, rog
0
by: J. Whitehurst | last post by:
I am currently trying to find a way to remotely execute an application such that the GUI will actually show up (and be interactive?) on the remote host. Simple example: have computer1 tell...
1
by: chris fink | last post by:
I am attempting to remote debug a process (C# application) whose execution time is less than 2 seconds. When attempting to attach this process from the client machine, I am unable to catch the...
2
by: RD | last post by:
I have a number of applications written bothe in vb.net and vb6 tat are running unattended 24-7 on sveral different machines on a lan at a client site. The problem is that sometimes one of these...
1
by: TerrenceJ | last post by:
Hi there I've configure our SBS 2003 server to allow remote access and Outlook over internet. The mail works fine, but when I try remote access, I am asked for username and password, which I...
2
by: jim.clifford | last post by:
Hello. I have a slow response with a system that I am setting up. The OS is Win 2000 Server with SQL Server 2000. My first execution of the SQL procedure is slow (about 40 seconds), while the...
0
by: triniman | last post by:
I am trying to start a qsub process on a remote server. This is not via http. This a case where the process is fired from one server that does not run qsubs and needs to go off to a server that runs...
1
by: Smokey Grindel | last post by:
Does anyone know if I run the Visual Studio Remote Debugger service on my development server will it slow down the system? Never used the service before... use to just launch the remote debugging...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.