Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run a batch file remotely with VBScript

Status
Not open for further replies.

monkeymagic2222

Technical User
May 13, 2002
78
GB
Hi,

I am new to VBScripting so don't know if this is possible but I have a script that copies some program files and folders to PC's on the domain by reading in a text file containing PC names. The copy part seems to works fine but I need the script to then run a batch file on the remote machine that registers some of the files that were copied. The batch file itself comes with the software and changes every time we receive an update so I can't hard encode the registry information into the VBScript, which is why I would like to call the batch file.

Heres my code so far, any information would be most appreciated.


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\scripts\computers.txt", ForReading)


Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")


Do Until objTextFile.AtEndOfStream
strComputers = objTextFile.ReadAll
Loop

objTextFile.Close

arrComputers = Split(strComputers, ",")

For Each strComputer in arrComputers

Set objShell = CreateObject("WScript.Shell")
strCommand = "%comspec% /c ping -n 3 -w 1000 " & strComputer & ""
Set objExecObject = objShell.Exec(strCommand)

Do While Not objExecObject.StdOut.AtEndOfStream
strText = objExecObject.StdOut.ReadAll()
If Instr(strText, "Reply") > 0 Then

'Loop

objFSO.CopyFile "\\Websense\ASNA_CIS\EB\ebviewindex.ini", "\\" & strComputer & "\c$\Windows\", true
objFSO.CreateFolder "\\" & strComputer & "\c$\syan"
objFSO.CreateFolder "\\" & strComputer & "\c$\syan\asna_cis"
objFSO.CopyFolder "\\websense\ASNA_CIS\*", "\\" & strComputer & "\c$\syan\ASNA_CIS\", true
objFSO.CopyFile "\\websense\ASNA_CIS\*.*", "\\" & strComputer & "\c$\syan\ASNA_CIS\", true

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set oprocess = objWMIService.Get("win32_process")

ret = oprocess.create("cmd.exe /c c:\syan\asna_cis\dll registry\register dll.bat", Null, Null, intProcessID)

' End loop

Else
Wscript.Echo strComputer & " could not be reached."
End If
Loop
Next
 
Change a bit to clean up.

[0] File's Readall should be script simpler.
[tt]
'Do Until objTextFile.AtEndOfStream
strComputers = objTextFile.ReadAll
'Loop
[/tt]
[1] Eliminate the Do While Not objExecObject.StdOut.AtEndOfStream - Loop. The readall is done through this and it contains _all_ in stdout.
[tt]
'Do While Not objExecObject.StdOut.AtEndOfStream
Do While objExecObject.Status = 0 : WScript.Sleep 100 : Loop
strText = objExecObject.StdOut.ReadAll
'other lines follow
'Loop 'eliminated do loop
[/tt]
[2] I would tend to give some time lag between each operation of copyfile or copyfolder. Insert for instance wscript.sleep with some reasonable time with your knowledge of the speed/traffic of your network.

[3] Your central question should have a positive answer. Only that you do not indicate any problem you encounter. Is there any problem?

- tsuji

 
Hi tsuji,

Thankyou for the useful info. The problem i'm having is the batch file doesn't seem to run on the remote machine.

I know the following command definately runs the cmd.exe on the machine but I don't know whats happening with the batch file. Its definately there and I can manually run it myself by clicking on it but i'd like the vbscript to call it to save the user clicking on it.

ret = oprocess.create("cmd.exe /c c:\syan\asna_cis\dll registry\register dll.bat", Null, Null, intProcessID)

The client machines all have Windows XP with SP2 installed so perhaps this is causing the problem? The user signed on to each machine has admin access to the section of the registry that is being updated so it shouldn't be a rights issue.

Thanks
 
mkmg2222,

Make sure you put the silent switch on /s if you use regsvr32. The process would not be allowed for interactive.

- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top