Run a reg file and a batch file on remote machine

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
LVL 11
bsharathAsked:
Who is Participating?
 
RobSampsonConnect With a Mentor Commented:
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
 
käµfm³d 👽Commented:
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
 
RobSampsonCommented:
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
bsharathAuthor Commented:
Thanks Rob where should i enter the machine names
0
 
RobSampsonCommented:
In this line:
strComputers = "\\server\share\computers.txt"

as a text file with one machine per line.

Rob.
0
 
bsharathAuthor Commented:
Rob but nothing happens. It keeps running and nothing gets created in the output folder
0
 
RobSampsonCommented:
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
 
bsharathAuthor Commented:
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
 
RobSampsonCommented:
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
 
bsharathAuthor Commented:
Rob now its running but not sure if success or failure
I am running just a bat within and commented all others
0
 
RobSampsonCommented:
Does any file get written to the log folder?

If not, is wscript.exe still running on the remote machine?
0
 
bsharathAuthor Commented:
Yes script is still running but no files are created in the output folder
0
 
RobSampsonCommented:
Just to test, what happens if you run the first script manually?
0
 
bsharathAuthor Commented:
It opened a command window and closed and even the process is stopped
0
 
bsharathAuthor Commented:
It did not ask me for any credentials as well
0
 
RobSampsonCommented:
Did it write anything to the log folder?  It should write something...
0
 
bsharathAuthor Commented:
You mean this folder

strOutputFolder?

Its empty
0
 
RobSampsonCommented:
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
 
bsharathAuthor Commented:
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
 
RobSampsonCommented:
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
 
bsharathAuthor Commented:
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
 
RobSampsonCommented:
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
 
bsharathAuthor Commented:
its not outputting anything
0
 
RobSampsonCommented:
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
 
RobSampsonCommented:
Ooops, take out line 11:
MsgBox strOutputFile

That's not supposed to be there.
0
 
RobSampsonCommented:
Hmmm, there's still an issue running it using PSExec....I'll iron that out tomorrow...
0
 
bsharathAuthor Commented:
Thanks a lot Rob
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.