Login | |
|
 |
RE: script to run a batch file on remote machine - 8/5/2005 5:37:12 AM
|
|
 |
|
| |
kirrilian
Posts: 628
Score: 3
Joined: 3/15/2005
From:
Status: offline
|
Running remote scripts with wsh is a PITA to setup, and very insecure. I agree with esnmb, check out psexec from sysinternals.
_____________________________
Have you searched here ? VBScript Fundamentals My Site
|
|
| |
|
|
|
 |
RE: script to run a batch file on remote machine - 8/5/2005 4:49:15 PM
|
|
 |
|
| |
Xandros
Posts: 100
Score: 0
Joined: 6/23/2005
From:
Status: offline
|
Here is a sample script I created last week that executes a batch file on a remote machine. This one doesn't have any error checking in it but it isn't that difficult to add some. The trickiest part of using WshRemote is getting DCOM properly configured on both the local machine and the remote machine. It's not terribly difficult but finding out what needs to be configured and where it is can be trying if you've never done it before and don't have a good set of instructions to follow. Here's the sample script... (note that the shares don't actually have to exist (and probably shouldn't when testing this) to run this sample... just change the value assigned to the variable "Server" to one of your own machines... and be sure the sharenames don't match or, when you get this to run, it WILL remove the shares!) ' This script generates a bat file on a remote machine, a vbs file on ' the local machine, then executes the local vbs file on the remote machine. ' The purpose of the generated files is to remove unneeded shares and to ' do it without using WMI. ' For demonstration purposes, the sharenames are assumed to be sequential ' "sharexxxx" where "xxxx" is a number. Option Explicit Dim Server Dim ShareCount, nShare, ShareName Dim OFSO, oFile, n, StartTime Dim Controller, RemoteScript StartTime = Timer() Wscript.Echo "Beginning removal of shares at: " & Now() Server = "galahad" ShareCount = 1000 nShare = 0 Set oFSO = CreateObject("Scripting.FileSystemObject") ' First, build the bat file with the "NET SHARE /DELETE <sharename>" commands... Set oFile = oFSO.CreateTextFile("\\" & Server & "\c$\temp\" & "_DelShare.bat") For n = 1 to ShareCount ShareName = "share" & n oFile.WriteLine "net share /delete " & ShareName Next oFile.Close ' Next, create a vbs file with commands to invoke the bat file... Set oFile = oFSO.CreateTextFile("C:\temp\_DelShare.vbs") oFile.WriteLine "WshShell.Run " & Chr(34) & "cmd /c c:\temp\_DelShare " oFile.Close ' Finally, invoke the remote script remotely on the server. Set Controller = WScript.CreateObject("WSHController") Set RemoteScript = Controller.CreateScript("c:\temp\_DelShare.vbs " & ShareName, "\\" & Server) RemoteScript.Execute Do While RemoteScript.Status <> 2 WScript.Sleep 100 Loop Set RemoteScript = Nothing Set Controller = Nothing Set oFile = Nothing Set oFSO = Nothing Wscript.Echo "Finished removal of shares at: " & Now() & " Elapsed time = " & Elapsed(StartTime, Timer()) Function Elapsed(tStart, tEnd) Dim HH, MM, SS If tEnd < tStart Then tEnd = tEnd + 86400 ' allow for possibility of crossing midnight one time. End If SS = tEnd - tStart If SS >= 3600 Then HH = Int(SS Mod 3600) SS = SS Mod 3600 Else HH = 0 End If If SS >= 60 Then MM = Int(SS Mod 60) SS = SS Mod 60 Else MM = 0 End If Elapsed = Right("00" & HH,2) & ":" & Right("00" & MM,2) & ":" & SS End Function
|
|
| |
|
|
|
| |
|
|
 |
|
 |
|
|