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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remotely Execute Batch File

Status
Not open for further replies.

chriswhitfield

Technical User
Jul 2, 2002
4
US
I am new to vbscript and have a problem. This is probably very simple but I have not been able to find documentation on it anywhere. I need to execute a batch file on a remote machine on a network. I have attempted to do this via the AT or SOON commands but have run into time zone issues with scheduling tasks. Is there a way to tell a remote shell to run a batch file?
 
chris,

I wrote this as a function. You can pass the server, account, password and the command you want to execute to the target server.

Cheers,
fengshui_1998

'****************************************
Function Remote_Cmd(Server, accnt, pwd, strCmd)
'****************************************
On Error Resume Next
Set Locator = CreateObject("WbemScripting.SWbemLocator")
user = server & "\" & accnt

Set Svc = Locator.ConnectServer(Server, "root\cimv2", user, pwd)
Svc.Security_.impersonationlevel = 3
Set Process = Svc.Get("Win32_Process")
intStatus = Process.Create("cmd /C " & strCmd, null, null, intProcessId)
' response.write err.number & " " & err.description
If err.number <> 0 then
Remote_Cmd = err.number
Else
Remote_Cmd = 0
End If
End Function
'
 
Just to verify that I change the correct items, are these the ones that I change to reflect my needs?

(Server, accnt, pwd, strCmd)

And if so, can I change these to variables that are pulled from the rest of my script?

Thanks for all your help.
 
chris,

Yes, just substitute the strings each time you call the function.

server = &quot;myservername&quot;
accnt = &quot;myaccount&quot;
pwd = &quot;mypasswd&quot;
strCmd = &quot;at 2:00PM C:\testdir\test.bat&quot;

ret = Remote_Cmd(server, accnt, pwd, strCMD)

fengshui_1998
 
Ahhh, that is what I was afraid of. I would still need the local system time for the remote system then. I am trying to execute the batch file immediately upon completion of the script without user intervention.
 
chris,

You can get the time difference and then use dateAdd function.

fengshui_1998
 
That is beyond my current level of knowledge. I only started scripting yesterday.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top