|
-
Aug 25th, 2010, 04:31 AM
#1
Thread Starter
Junior Member
[RESOLVED] Remote Login and starting a batch file
Hello everyone,
I am a newbie to VB. I would appreciate if someone would help me out.
I am try to achieve the following
1) Remote login to a PC
2)Check if a specific program has terminated
3)If it is , the program has to restart the program file located on the desktop.
Thanks in advance
-
Aug 25th, 2010, 04:38 AM
#2
Re: Remote Login and starting a batch file
Explain remote login to a PC, how do you wish to do this? Remote Desktop, telnet?
-
Aug 25th, 2010, 04:47 AM
#3
Thread Starter
Junior Member
Re: Remote Login and starting a batch file
Normally the client calls me to let me know the timetable (which the batch file run) has stopped working. Normally I login using remote desktop to the PC to restart the batch file that runs the timetable. It would be a lot easier with a vb program which i can instruct the client to just run the file (without having to give the admin name and pass to the remote pc), in which case the program would check if the batch file has stopped and if it has stopped would automatically run the batch file.
So whenever the client notices the timetable has stopped, they can run the vb program, without any further interaction from the client.
-
Aug 25th, 2010, 05:13 AM
#4
Re: Remote Login and starting a batch file
You have a few options depending on where you are "running" from.
You could just use windows scheduler to run an application that checks for this process and restart it, this is all local on each machine.
You could write your own windows service that does the same thing.
You could write a server application that uses WMI to remotely check if a machine is running a process and attempt to start one (I have not tried this remotly, not sure how possible it is). A powershell script would be the simplist option as it does have the ability to check for processes and start processes.
There are a number of scheduler/automation apps on the market (my company makes one ) but they would be overkill for just one process.
-
Aug 25th, 2010, 05:33 AM
#5
Thread Starter
Junior Member
Re: Remote Login and starting a batch file
The PC that runs the batch file is on the same network. Also the PC on which the batch file resides is a WinXP machine.
I will be trying to remote login from a windows Xp machine also.
If it is possible how do i do the 3rd or the 4th option u suggested.
I heard PsExec can achieve this. Couldnt it be possible to code a vb program that executes the psexec with the parameters to run the batch file on the remote machine. Only problem is how to check if the batch file is already running or not on the PC.
-
Aug 25th, 2010, 05:43 AM
#6
Re: Remote Login and starting a batch file
You can use WMI and check for a running process, theres a good article in the codebank for it. A slight tweak and you can add a WHERE clause into it to only select the process you want. This will give you either 0 (not running) or more than 0 (running).
I also found a thread here, that shows an example of running a process remotly using WMI, but I have never tried it.
-
Aug 25th, 2010, 06:20 AM
#7
Thread Starter
Junior Member
Re: Remote Login and starting a batch file
Can you show me how the clause should be modified?
-
Aug 25th, 2010, 06:28 AM
#8
Re: Remote Login and starting a batch file
Dim search As New ManagementObjectSearcher("SELECT * FROM Win32_process")
That is selecting all processes, its just a query that you can modify like any t-sql (its called WQL apparently, but works the same to me!). So you can say:
Select ProcessID FROM Win32_process Where Name = "your app name.exe"
Something like that. Use the codebank sample to view all of the processes until you get to your one and note down the fields that identify your app.
-
Aug 25th, 2010, 06:39 AM
#9
Thread Starter
Junior Member
Re: Remote Login and starting a batch file
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim search As New ManagementObjectSearcher("SELECT ProcessID FROM Win32_process WHERE Name = explorer.exe")
Dim info As ManagementObject
For Each info In search.Get()
MessageBox.Show(info.GetText(TextFormat.Mof).ToString, "Running Processes Information", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
Next
End Sub
Doesnt Work. I took explorer.exe as an example. I know i am making a mistake somewhere
Last edited by resmiac; Aug 25th, 2010 at 06:48 AM.
-
Aug 25th, 2010, 06:56 AM
#10
Re: Remote Login and starting a batch file
Your are missing quotes around your "explorer.exe", you have to doubleup to use them in a string (or even triple at begining or end)
("SELECT * FROM Win32_process WHERE Name = ""explorer.exe""")
Use * for all fields or you wont see the data to help you identify the app. You only have to return 1 field when you get your real value as you don't care what value it has, just that you have something.
Last edited by Grimfort; Aug 25th, 2010 at 07:02 AM.
-
Aug 25th, 2010, 10:26 AM
#11
Lively Member
Re: Remote Login and starting a batch file
this would be a lot easier with a batch file. use psexec to connect to a computer and run your program.
psexec is a part of pstools, which can be found with google.
with psexec, you will have a running batch file in minutes.
-
Aug 25th, 2010, 11:55 AM
#12
Thread Starter
Junior Member
Re: Remote Login and starting a batch file
Code:
Private Sub RunRemoteProcess()
Dim sCmd As String = "C:\apache-tomcat-6.0.18\bin\startup.bat"
' add a reference to System.Management in Solution Explorer
Dim wmi As ManagementClass
Dim wmi_in, wmi_out As ManagementBaseObject
Dim retValue As Integer
Dim imp As New System.Management.ConnectionOptions
imp.Username = "administrator"
imp.Password = "password"
imp.Impersonation = Management.ImpersonationLevel.Impersonate
imp.Authentication = Management.AuthenticationLevel.PacketPrivacy
Try
wmi = New ManagementClass("\\userpc\root\cimv2:Win32_Process")
' get the parameters to the Create method
wmi_in = wmi.GetMethodParameters("Create")
' fill in the command line plus any command-line arguments
' NOTE: the command can NOT be on a network resource!
wmi_in("CommandLine") = sCmd
' do it!
wmi_out = wmi.InvokeMethod("Create", wmi_in, Nothing)
' get the return code. This not the return code of the
' application... it's a return code for the WMI method
retValue = Convert.ToInt32(wmi_out("returnValue"))
Select Case retValue
Case 0
' success!
Case 2
Throw New ApplicationException("Access denied")
Case 3
Throw New ApplicationException("Insufficient privilege")
Case 8
Throw New ApplicationException("Unknown failure")
Case 9
Throw New ApplicationException("Path not found")
Case 21
Throw New ApplicationException("Invalid parameter")
Case Else
Throw New ApplicationException("Unknown return code " & retValue)
End Select
Catch ex As Exception
MsgBox("Can't create the process. " & ex.Message)
End Try
End Sub
I keep getting "Cant Create the process. Access denied (Exception from HRESULT:0X80070005(E_ACCESSDENIED))
-
Aug 25th, 2010, 12:49 PM
#13
Thread Starter
Junior Member
Re: Remote Login and starting a batch file
Code:
Option Explicit On
Option Strict On
'Add a reference to System.Management
Imports System.Management
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim count As Integer = 0
Dim search As New ManagementObjectSearcher("SELECT * FROM Win32_process WHERE Name = ""startup.bat""")
Dim info As ManagementObject
For Each info In search.Get()
count = 1
Next
If count = 0 Then
Process.Start("C:\apache-tomcat-6.0.18\bin\startup.bat")
Else
MsgBox("Already running!")
End If
End Sub
End Class
Everything works fine. But the problem is after it opens it executes the batch file "startup.bat" but also the batch file terminates immediately instead of staying open. Any idea why? Normally if i normally double click on the batch file it stays opens.
-
Aug 25th, 2010, 02:21 PM
#14
Re: Remote Login and starting a batch file
I assume that your running this locally as a test?
Does your batch file do things in the local directory, ie Runtthis.exe instead of C:\MyFolder\Runtthis.exe
that could fail if you are not in the correct location. Its the same as running that exact command in a dos prompt in a location like C:\ instead of inside the folder.
-
Aug 25th, 2010, 03:17 PM
#15
Thread Starter
Junior Member
Re: Remote Login and starting a batch file
Thanks for the reply. Yes I am testing it locally.
I didnt follow ur question. But if ur asking if the startup.bat is located in the same folder as the other files it requires. Yes it is. the batch file is a startup script for a Catalina server. Normally I double click and it stays open. With the above code I posted above i get a message "Are you sure u want to run the following file ....." which is how i know tat the code called the program. Once i click yes the batch file should start running and stay open but instead it closes as soon as it opens.
-
Aug 25th, 2010, 06:16 PM
#16
Re: Remote Login and starting a batch file
So the command you use requires UAC access which will require the user to accept it?
-
Aug 26th, 2010, 01:58 AM
#17
Thread Starter
Junior Member
Re: Remote Login and starting a batch file
The prompt is a normal windows prompt tat asks do u want to run the program and at the bottom there is an option "Always ask before opening this type of file" checkbox. If i uncheck it will not ask for a prompt. But why does the batch file close when i open through VB?
-
Aug 26th, 2010, 03:39 AM
#18
Re: Remote Login and starting a batch file
I've suggested twice that your batch file needs a workingdirectory setup. Add a 'Pause' at the end of your batch file so you can see what its doing, I can only guess you will get 'command not found' errors as your directory that runs the batch file will be windows and the commands you are running are not in the windows directory.
-
Aug 26th, 2010, 04:11 AM
#19
Thread Starter
Junior Member
Re: Remote Login and starting a batch file
I will check it tonight when i get bac to the system and let u know. BTW how do i make the code check remotely if the batch file is running. This works locally.
-
Aug 26th, 2010, 04:17 AM
#20
Re: Remote Login and starting a batch file
The ManagementObjectSeacher takes a parameter called ManagementScope that you can pass in the name of the server to interrogate. There are examples in both those links.
-
Aug 27th, 2010, 05:51 AM
#21
Thread Starter
Junior Member
Re: Remote Login and starting a batch file
Code:
Function RemoteConnect()
Dim options As ConnectionOptions
options = New ConnectionOptions()
options.Username = "192.168.1.100\administrator"
options.Password = "password"
'options.Authority = "ntlmdomain:DOMAIN"
Dim scope As ManagementScope
scope = New ManagementScope( _
"\\192.168.1.100\root\cimv2", options)
scope.Connect()
Dim query As ObjectQuery
query = New ObjectQuery( _
"SELECT * FROM Win32_process WHERE Name = ""java.exe""")
Dim searcher As ManagementObjectSearcher
searcher = _
New ManagementObjectSearcher(scope, query)
Dim queryCollection As ManagementObjectCollection
queryCollection = searcher.Get()
Dim count As Integer = 0
Dim m As ManagementObject
For Each m In queryCollection
count = 1
Next
If count = 0 Then
' New ProcessStartInfo created
Dim p As New ProcessStartInfo
p.FileName = "C:\PsExec.exe"
p.Arguments = "\\192.168.1.100 -u administrator -p password -i -d calc.exe"
p.WindowStyle = ProcessWindowStyle.Hidden
Process.Start(p)
End If
Return 0
End Function
Ok i got around to making this work successfully using PSExec when i tried to run it on a test machine remotely. When I tried to connect on the actual target machine, I encountered 2 problems
1) Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
2)PsExec - No Process is on the other end of the pipe error
The only thing running on that system is ESET Nod32 antivirus, i turned off the firewall. Any idea how i can resolve this?
-
Aug 27th, 2010, 09:42 AM
#22
Re: Remote Login and starting a batch file
I've never used that tool before, but a quick search shows that the username if its a domain user has to be qualified as such:
-u \\mydomain\administrator
-
Aug 27th, 2010, 09:56 AM
#23
Re: Remote Login and starting a batch file
 Originally Posted by Grimfort
I've never used that tool before, but a quick search shows that the username if its a domain user has to be qualified as such:
-u \\mydomain\administrator
You would never qualify a domain username like that so I doubt that is the format it expects it in - that would be for a user account that is local to the PC you are trying to access.
A domain user would be: MYDOMAIN\Administrator
Local user would be: \\ComputerName\Administrator
-
Aug 27th, 2010, 10:00 AM
#24
Re: Remote Login and starting a batch file
Do apologise, that was for the first param, I got trigger happy.
-
Aug 29th, 2010, 02:07 PM
#25
Thread Starter
Junior Member
Re: Remote Login and starting a batch file
Its k I found out the Nod32 was causing the problem. The above code works... As an alternative to PsExec u can use another utility called xCmd also, it works thru the Nod32 problem, but it does not start the app interactively. Thanks for the patient replies Grimfort. The thread can be closed.
-
Aug 29th, 2010, 03:04 PM
#26
Re: Remote Login and starting a batch file
As the OP thats for you to resolve .
-
Aug 29th, 2010, 04:50 PM
#27
Re: Remote Login and starting a batch file
Yeah just go to the Thread Tools menu at the top of the page and select Mark Thread Resolved
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|