Solved

VB Script to run batch file on another machine

Posted on 2011-02-18
6
619 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:asmyatt
  • 4
  • 2
6 Comments
 
LVL 10

Expert Comment

by:answer_dude
ID: 34928194
Here are two options:  the short one will just run it with whatever local permissions you already have, the longer snippet will ask you for the ID/Password that you want to "run as"

dim objShell
set objShell=createobject("wscript.shell")
objShell.run "\\servername\sharedfolder\batch.bat"
set objShell=nothing

Open in new window

Option Explicit

Dim objShell, FSO, strBatchFile

Set objShell = CreateObject("Shell.Application")
Set FSO = CreateObject("Scripting.FileSystemObject")
strBatchFile = "\\servername\sharedfolder\batch.bat"
If FSO.FileExists(strBatchFile) Then
     objShell.ShellExecute "wscript.exe", _ 
        Chr(34) & strBatchFile & Chr(34), "", "runas", 1
Else
     MsgBox "Script file " & strBatchFile & " not found"
End If

Set objShell = Nothing

Open in new window

0
 
LVL 2

Author Comment

by:asmyatt
ID: 34928552
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.
0
 
LVL 10

Expert Comment

by:answer_dude
ID: 34929162
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.
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

Open in new window

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 2

Author Comment

by:asmyatt
ID: 34929493
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

Open in new window

0
 
LVL 10

Expert Comment

by:answer_dude
ID: 34929947
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.
0
 
LVL 10

Accepted Solution

by:
answer_dude earned 500 total points
ID: 34930026
Actually -- after some poking around, I found something that might work... try this:
Option Explicit

Dim strComputer, strCommand, objProcess, objWMIService, errReturn, objWbemLocator

strComputer = "servername"
strCommand = "\\servername\sharedfolder\batch.bat"

strNamespace = "root\cimv2"
strUser = inputbox("username")
strPassword = inputbox("password")
Set objWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = objwbemLocator.ConnectServer _
(strComputer, strNamespace, strUser, strPassword)

'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

Open in new window

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now