Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


script to run a batch file on remote machine

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,24477
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> script to run a batch file on remote machine
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 script to run a batch file on remote machine - 8/5/2005 2:24:47 AM   
  aiu0835

 

Posts: 4
Score: 0
Joined: 6/8/2005
From: USA
Status: offline
I need a script to run a batch file on a remote machine from another machine and would also do some error checking to make sure the batch file runs.  I'm a beginner in WSH, so it will take me a long time to figure this out.
 
 
Post #: 1
 
 RE: script to run a batch file on remote machine - 8/5/2005 5:28:18 AM   
  esnmb

 

Posts: 441
Score: 0
Joined: 1/11/2005
From: USA
Status: offline
If the batch file is already created, I would just check out PSExec from SysInternals.

(in reply to aiu0835)
 
 
Post #: 2
 
 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

(in reply to aiu0835)
 
 
Post #: 3
 
 RE: script to run a batch file on remote machine - 8/5/2005 6:14:25 AM   
  gdmix

 

Posts: 5
Score: 0
Joined: 8/2/2005
Status: offline
What I do with Psexec rather than copy the batch file and any other files to the machine(s) is have the system run the batch file from a share. You need to supply credentials for this.

Here is an example:
psexec @Host-List.txt -u domain\administrator -p <password> -d -i "\\server\share\batch.cmd"


Put any files to be run on a share also, unless it's something already present. I like this method much better than using "-c". It also allows you to run more complex tasks. The credentials you supply needs to have rights to run the task on the PC and access the shares.

(in reply to kirrilian)
 
 
Post #: 4
 
 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

(in reply to aiu0835)
 
 
Post #: 5
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> script to run a batch file on remote machine Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts