?
Solved

Run a reg file and a batch file on remote machine

Posted on 2011-05-11
28
Medium Priority
?
966 Views
Last Modified: 2012-05-11
Hi,

Run a reg file and a batch file on remote machine
I have a requirment where i need to run one registry file and one batch script and a exe. the exe's output has to come into a UNC path with machine name as file name.

Can anyone help i need to do this on 1000+ machines and need real quick help.

Regards
Raja
0
Comment
Question by:bsharath
  • 14
  • 13
28 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35743776
I would say look into the utilities at SysInternals, particularly PsExec. Using that utility, you should be able to write a small script to run against each machine.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35743928
Hi, with your computer names in computers.txt, adjust the paths at the top of this script, and test it out.  It uses PSExec to run the programs on each remote system.

Make sure you run it using
cscript RunProgramsRemotely.vbs

otherwise you might get a lot of message boxes on screen.

Regards,

Rob.
strComputers = "\\server\share\computers.txt"
strRegFile = "\\server\share\MyRegFile.reg"
strBatchFile = "\\server\share\MyBatchFile.bat"
strExeFile = "\\server\share\MyExeFile.exe"
strPSExec = "\\server\share\psexec.exe"
strOutputFolder = "\\server\share\output\"
strUsername = "domain\username"

strPassword = InputBox("Enter the password for " & strUsername & ":")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Set objShell = CreateObject("WScript.Shell")
If Right(strOutput, 1) <> "\" Then strOutput = strOutput & "\"
strPSExec = objFSO.GetFile(strPSExec).ShortPath
Set objComputers = objFSO.OpenTextFile(strComputers, intForReading, False)
While Not objComputers.AtEndOfStream
	strServer = objComputers.ReadLine
	If Ping(strServer) = True Then
		' Run reg file on remote system
		strCommand = "cmd /c " & strPSExec & " -accepteula -e -u " & strUsername & " -p " & strPassword & " \\" & strServer & " regedit /s """ & strRegFile & """"
		objShell.Run strCommand, 0, True
		' Run batch file on remote system
		strCommand = "cmd /c " & strPSExec & " -accepteula -e -u " & strUsername & " -p " & strPassword & " \\" & strServer & " cmd /c """ & strBatchFile & """"
		objShell.Run strCommand, 0, True
		' Run exe file on remote system
		strOutputFile = strOutputFolder & strServer & ".txt"
		strCommand = "cmd /c " & strPSExec & " -accepteula -e -u " & strUsername & " -p " & strPassword & " \\" & strServer & " cmd /c """ & strExeFile & """ > """ & strOutputFile & """"
		objShell.Run strCommand, 0, True
	Else
		WScript.Echo strServer & " is offline."
	End If
Wend
objComputers.Close

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
ID: 35743989
Thanks Rob where should i enter the machine names
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 65

Expert Comment

by:RobSampson
ID: 35744097
In this line:
strComputers = "\\server\share\computers.txt"

as a text file with one machine per line.

Rob.
0
 
LVL 11

Author Comment

by:bsharath
ID: 35744158
Rob but nothing happens. It keeps running and nothing gets created in the output folder
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35745457
OK, run this against only a few machines (or one or two), and see the output on screen from each PSExec command.  It will show you what the exit code from PSExec is for each of the three commands against each machine.

Regards,

Rob.
strComputers = "\\server\share\computers.txt"
strRegFile = "\\server\share\MyRegFile.reg"
strBatchFile = "\\server\share\MyBatchFile.bat"
strExeFile = "\\server\share\MyExeFile.exe"
strPSExec = "\\server\share\psexec.exe"
strOutputFolder = "\\server\share\output\"
strUsername = "domain\username"

strPassword = InputBox("Enter the password for " & strUsername & ":")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Set objShell = CreateObject("WScript.Shell")
If Right(strOutput, 1) <> "\" Then strOutput = strOutput & "\"
strPSExec = objFSO.GetFile(strPSExec).ShortPath
Set objComputers = objFSO.OpenTextFile(strComputers, intForReading, False)
While Not objComputers.AtEndOfStream
	strServer = objComputers.ReadLine
	If Ping(strServer) = True Then
		' Run reg file on remote system
		strCommand = "cmd /k " & strPSExec & " -accepteula -e -u " & strUsername & " -p " & strPassword & " \\" & strServer & " regedit /s """ & strRegFile & """"
		objShell.Run strCommand, 1, True
		' Run batch file on remote system
		strCommand = "cmd /k " & strPSExec & " -accepteula -e -u " & strUsername & " -p " & strPassword & " \\" & strServer & " cmd /c """ & strBatchFile & """"
		objShell.Run strCommand, 1, True
		' Run exe file on remote system
		strOutputFile = strOutputFolder & strServer & ".txt"
		strCommand = "cmd /k " & strPSExec & " -accepteula -e -u " & strUsername & " -p " & strPassword & " \\" & strServer & " cmd /c """ & strExeFile & """ > """ & strOutputFile & """"
		objShell.Run strCommand, 1, True
	Else
		WScript.Echo strServer & " is offline."
	End If
Wend
objComputers.Close

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
ID: 35745910
Rob within the batch file i have

Gpupdate /force

it gets stuck in this process and when esc it runs the exe which is clientdiag.exe the script creates the txt file but empty file even this does not continue and i need to esc
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35751285
Why do they hang?  As far as I can tell, the only way to get around that is to wrap it in a VBScript, and run that on the remote machine.  The VBScript would be able to time out the commands if they took too long.

So, save this VBS code on a network share, which will be run by a parent script.

strRegFile = "\\server\share\MyRegFile.reg"
strBatchFile = "\\server\share\MyBatchFile.bat"
strExeFile = "\\server\share\MyExeFile.exe"
strOutputFolder = "\\server\share\output\"
' Specify the command time out in seconds
intTimeOut = 60
If Right(strOutput, 1) <> "\" Then strOutput = strOutput & "\"
Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
strOutputFile = strOutput & objNetwork.ComputerName & ".txt"
' Run the registry import
intReturn = objShell.Run("regedit /s """ & strRegFile & """", 0, True)
If intReturn = 0 Then
	objShell.Run "cmd /c echo Registry import successful > """ & strOutputFile & """", 0, True
Else
	objShell.Run "cmd /c echo Registry import failed > """ & strOutputFile & """", 0, True
End If
' Run the batch file using the time out value
Set objExec = objShell.Exec("cmd /c """ & strBatchFile & """")
intSeconds = 0
While objExec.Status = 0 And intSeconds < intTimeOut
	intSeconds = intSeconds + 1
	WScript.Sleep 1000
Wend
If objExec.Status = 0 Then
	objExec.Terminate
	objShell.Run "cmd /c echo Batch file terminated after " & intTimeOut & " seconds >> """ & strOutputFile & """", 0, True
Else
	objShell.Run "cmd /c echo Batch file successful >> """ & strOutputFile & """", 0, True
End If
' Run the executable file using the time out value
Set objExec = objShell.Exec("cmd /c """ & strExeFile & """ >> """ & strOutputFile & """")
intSeconds = 0
While objExec.Status = 0 And intSeconds < intTimeOut
	intSeconds = intSeconds + 1
	WScript.Sleep 1000
Wend
If objExec.Status = 0 Then
	objExec.Terminate
	objShell.Run "cmd /c echo Exe file terminated after " & intTimeOut & " seconds >> """ & strOutputFile & """", 0, True
Else
	objShell.Run "cmd /c echo Exe file successful >> """ & strOutputFile & """", 0, True
End If

Open in new window


Then, point this parent script to the script above with strVBSFile:
strComputers = "\\server\share\computers.txt"
strPSExec = "\\server\share\psexec.exe"
strVBSFile = "\\server\share\MyVBSFile.vbs"
strUsername = "domain\username"

strPassword = InputBox("Enter the password for " & strUsername & ":")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Set objShell = CreateObject("WScript.Shell")
strPSExec = objFSO.GetFile(strPSExec).ShortPath
Set objComputers = objFSO.OpenTextFile(strComputers, intForReading, False)
While Not objComputers.AtEndOfStream
	strServer = objComputers.ReadLine
	If Ping(strServer) = True Then
		' Run vbs file on remote system
		strCommand = "cmd /c " & strPSExec & " -accepteula -e -u " & strUsername & " -p " & strPassword & " \\" & strServer & " wscript.exe //B """ & strVBSFile & """"
		objShell.Run strCommand, 0, True
	Else
		WScript.Echo strServer & " is offline."
	End If
Wend
objComputers.Close

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window


and then run the parent script only (the second code snippet).  This will read from your text file, and execute the first VBS file on each remote computer.  It should output to the output folder that you specify in the first script.

Regards,

Rob.
0
 
LVL 11

Author Comment

by:bsharath
ID: 35751940
Rob now its running but not sure if success or failure
I am running just a bat within and commented all others
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35751975
Does any file get written to the log folder?

If not, is wscript.exe still running on the remote machine?
0
 
LVL 11

Author Comment

by:bsharath
ID: 35752026
Yes script is still running but no files are created in the output folder
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35752048
Just to test, what happens if you run the first script manually?
0
 
LVL 11

Author Comment

by:bsharath
ID: 35752068
It opened a command window and closed and even the process is stopped
0
 
LVL 11

Author Comment

by:bsharath
ID: 35752069
It did not ask me for any credentials as well
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35752108
Did it write anything to the log folder?  It should write something...
0
 
LVL 11

Author Comment

by:bsharath
ID: 35752128
You mean this folder

strOutputFolder?

Its empty
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35752141
Oh no!  I just found a typo!!!

Change this line:
strOutputFile = strOutput & objNetwork.ComputerName & ".txt"

to this
strOutputFile = strOutputFolder & objNetwork.ComputerName & ".txt"

and see what happens then...
0
 
LVL 11

Author Comment

by:bsharath
ID: 35752174
The first vbs i have just this line
strBatchFile = "\\inds\All\Soft\wsus.bat"


The 2nd has the below

strComputers = "\\inds\All\Soft\computers.txt"
strPSExec = "\\inds\All\Soft\psexec.exe"
strVBSFile = "\\inds\All\Soft\Run_Reg_bat_exe.vbs"
strUsername = "ieee\sin"

Wscript.exe is still running

No message at all
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35752250
If you change the line I mentioned, and run the first script on its own manually on one computer, it should output something to that folder.
0
 
LVL 11

Author Comment

by:bsharath
ID: 35752280
If i take the first script as it is. The computers and psexec is not called is that fine.

strRegFile = "\\server\share\MyRegFile.reg"
strBatchFile = "\\server\share\MyBatchFile.bat"
strExeFile = "\\server\share\MyExeFile.exe"
strOutputFolder = "\\server\share\output\"
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35759265
Yes, that's right.  The first script will run against one computer only.  It's the second that uses the text file and psexec to process the first on each machine.

So just test the first one with those four lines changed on one machine.  We need to make sure it's outputting correctly.

Rob.
0
 
LVL 11

Author Comment

by:bsharath
ID: 35760064
its not outputting anything
0
 
LVL 11

Author Comment

by:bsharath
ID: 35766494
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35767277
Try this for script 1 (the one that doesn't use PSExec)

You must make sure that the output folder that you specify already exists.

Regards,

Rob.
strRegFile = "\\server\share\MyRegFile.reg"
strBatchFile = "\\server\share\MyBatchFile.bat"
strExeFile = "\\server\share\MyExeFile.exe"
strOutputFolder = "\\server\share\output\"
' Specify the command time out in seconds
intTimeOut = 60
If Right(strOutputFolder, 1) <> "\" Then strOutputFolder = strOutputFolder & "\"
Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
strOutputFile = strOutputFolder & objNetwork.ComputerName & ".txt"
MsgBox strOutputFile
' Run the registry import
intReturn = objShell.Run("regedit /s """ & strRegFile & """", 0, True)
If intReturn = 0 Then
	objShell.Run "cmd /c echo Registry import successful > """ & strOutputFile & """", 0, True
Else
	objShell.Run "cmd /c echo Registry import failed > """ & strOutputFile & """", 0, True
End If
' Run the batch file using the time out value
Set objExec = objShell.Exec("cmd /c """ & strBatchFile & """")
intSeconds = 0
While objExec.Status = 0 And intSeconds < intTimeOut
	intSeconds = intSeconds + 1
	WScript.Sleep 1000
Wend
If objExec.Status = 0 Then
	objExec.Terminate
	objShell.Run "cmd /c echo Batch file terminated after " & intTimeOut & " seconds >> """ & strOutputFile & """", 0, True
Else
	objShell.Run "cmd /c echo Batch file successful >> """ & strOutputFile & """", 0, True
End If
' Run the executable file using the time out value
Set objExec = objShell.Exec("cmd /c """ & strExeFile & """ >> """ & strOutputFile & """")
intSeconds = 0
While objExec.Status = 0 And intSeconds < intTimeOut
	intSeconds = intSeconds + 1
	WScript.Sleep 1000
Wend
If objExec.Status = 0 Then
	objExec.Terminate
	objShell.Run "cmd /c echo Exe file terminated after " & intTimeOut & " seconds >> """ & strOutputFile & """", 0, True
Else
	objShell.Run "cmd /c echo Exe file successful >> """ & strOutputFile & """", 0, True
End If

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35767307
Ooops, take out line 11:
MsgBox strOutputFile

That's not supposed to be there.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35767324
Hmmm, there's still an issue running it using PSExec....I'll iron that out tomorrow...
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 35774539
My issue was that I had the path to the first script incorrect in this second script.  Once I corrected the path, everything works fine.

Run this as the "parent" script, with the code above being the "child" script.  Don't forget to remove the MsgBox strOutputFile from the "child" script.

This parent script will show you the outcome of each PSExec command, so only test against a couple of machines.  If you're happy with that, and everything works, then change the cmd /k parts to cmd /c and the
objShell.Run strCommand, 1, True
lines to
objShell.Run strCommand, 0, True

Regards,

Rob.
strComputers = "\\server\share\Scripts\computers.txt"
strPSExec = "\\server\share\Scripts\psexec.exe"
strVBSFile = "\\server\share\Scripts\ChildScript.vbs"
strUsername = "domain\administrator"

strPassword = InputBox("Enter the password for " & strUsername & ":")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Set objShell = CreateObject("WScript.Shell")
strPSExec = objFSO.GetFile(strPSExec).ShortPath
Set objComputers = objFSO.OpenTextFile(strComputers, intForReading, False)
While Not objComputers.AtEndOfStream
	strServer = objComputers.ReadLine
	If Ping(strServer) = True Then
		' Run vbs file on remote system
		strCommand = "cmd /k " & strPSExec & " -accepteula -e -u " & strUsername & " -p " & strPassword & " \\" & strServer & " wscript.exe //B """ & strVBSFile & """"
		'InputBox "Prompt", "Title", strCommand
		objShell.Run strCommand, 1, True
	Else
		WScript.Echo strServer & " is offline."
	End If
Wend
objComputers.Close

MsgBox "Done"

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
ID: 35877150
Thanks a lot Rob
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question