asmyatt
asked on
VB Script to run batch file on another machine
Hi experts,
I have a batch file on a server that I need to run from my machine. What code in VB script can I use on my machine to execute the batch file on the server ( \\servername\sharedfolder\ batch.bat) ? It would need to work the same way as if I was on the server and ran the batch file.
Thanks.
I have a batch file on a server that I need to run from my machine. What code in VB script can I use on my machine to execute the batch file on the server ( \\servername\sharedfolder\
Thanks.
ASKER
Are these trying to execute the batch file on the server? It appears it's trying to run the batch file from the machine I'm runing the .vbs from.
It's trying to run the batch file on the server from your machine.
The batch file on the server, if it make calls to environment variables, etc., would in fact pull those from your machine.
If you want the batch file to be unaware of your machine... then you're out of my depth. I did find this code that might work (see attached).
But I didn't write it -- I found it here:
http://motevich.blogspot.com/2007/11/execute-program-on-remote-computer.html
I modified it to reference your batch file.
The batch file on the server, if it make calls to environment variables, etc., would in fact pull those from your machine.
If you want the batch file to be unaware of your machine... then you're out of my depth. I did find this code that might work (see attached).
But I didn't write it -- I found it here:
http://motevich.blogspot.com/2007/11/execute-program-on-remote-computer.html
I modified it to reference your batch file.
Option Explicit
Dim strComputer, strCommand, objProcess, objWMIService, errReturn
strComputer = "servername"
strCommand = "\\servername\sharedfolder\batch.bat"
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objProcess = objWMIService.Get("Win32_Process")
errReturn = objProcess.Create(strCommand, null, null, intProcessID)
If errReturn = 0 Then
Wscript.Echo strCommand & " was started with a process ID: " & intProcessID
Else
Wscript.Echo strCommand & " could not be started due to error: " & errReturn
End If
Set objWMIService = Nothing
Set objProcess = Nothing
ASKER
Ok, this code seems to work, but it's trying to run the batch file under my userid logged into the local machine. Is there a way to impersonate in the code a userid to use to run the batch file?
strCommand = "C:\crystal\test.bat"
strPath = "C:\temp"
strcomputer="servername"
process = "winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2"
msgbox process
Set objWMIService = GetObject(process)
Set objProcess = objWMIService.Get("Win32_Process")
errReturn = objProcess.Create(strCommand, strPath, Null, intProcessID)
If errReturn = 0 Then
WScript.Echo "scan success: " & intProcessID
Else
WScript.Echo "scan fail: " & errReturn
End If
Not that I am aware; but if you are able to run the batch file from your computer as if you were logged into the other computer, wouldn't you want it under your ID? Otherwise, you'd need to embed the other user IDs password into your script -- which would be risky.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window
Open in new window