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!

Executing batch files remotely 2

Status
Not open for further replies.
Oct 10, 2003
90
US
I don't mean to sound lazy, but I am in serious trouble and could use some fast help.

I know the topic has been covered previously, but I can't seem to understand how to make it work correctly. Using a previously working script, I can sucessfully create a folder on a remote machine, copy in all of July's hotfixes from Microsoft as well as a batch file to install all the hotfixes. The batch file works if I connect to each machine and execute it, but I am unable to cause the batch to run on the remote computer from a script running on my own computer.

The line I am currently working with is

objShell.Run("\\" & strComputer & "\c:\updates\patch.bat"),1,True

... but I error out with "The system cannot find the path specified" I have searched through the forum, tried the PSEXEC approach also, but can't seem to make that work either.

So, how can I get a batch file that I successfully copied to another computer to run on that computer???
 
To correct the path issue, replace this:
objShell.Run("\\" & strComputer & "\c:\updates\patch.bat"),1,True
By this:
objShell.Run("\\" & strComputer & "\c$\updates\patch.bat"),1,True

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
desktoprat,

Ok, I had not paid much attention to your last sentence. But, psexec will get it through. If I get your environment right, you connect to the remote through winmgmts: already. In that case, you can create the process at the remote, that would be simplest.
Code:
strComputer="a1b2c3"   'your input here
set svc=getobject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\cimv2")
set oproc=svc.get("win32_process")
iret=oproc.create("c:\updates\patch.bat")
set oproc=nothing
set svc=nothing
You can have more control encoded, but this is the frame you can work on with. (Just watch out: no interactive instruction.)

- tsuji
 
I really appreciate the both of your help. I am now back at work tonight and seeing what I can do.

PHV - I tried fixing the typo, but no go as of yet. So far, each time it gets to a new machine, it executes the batch file on my own machine. Well, it appears to be running on my own machine at least. Suggestion?

Tsuji - Everything appears to be working beautifully. Now on to adding in other niceties (i.e. logging)

Any chance of you two and/or Markdmac moving to Houston and teaching me even more??? I'd buy you dinner once a week.
 
desktoprat,

Thanks for the invitation.

The problem of the .run as such is that it will load executable to the address space as the caller can see. Hence, it is executed on _your_ own machine. And that is the behavior one should expect.

- tsuji
 
For my own edification, could you explain the line

iret=oproc.create("c:\updates\patch.bat")

1. What causes iret to fire?
2. Is it's reference to c: defined by the getObject line? I mean, how does it know you mean the remote computer vs the local computer?

Everytime I pick up something in this forum, I start thinking of previous scripts and what I can/could have done differently.
 
Another question.

If I did this

iret=oproc.create("c:\updates\patch.bat")
objFso.DeleteFolder(strFolder)

Will the batch file finish running before the folder its in gets deleted?

I am thinking that the folder will get deleted whether or not the batch is done running because the script is executing on its own, the batch file is doing its own thing on the other end. Therefore I should be ok by putting in a sleep line for a few seconds.
 
desktoprat,

It will not be doing so. You have to implement further control. I can sketch it for you. Create the process from the .bat and retrieve its processid. From the processid, verify the status of the process. If it is still running, loop and wait.

- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top