VBScript Function to Run Program at Remote Computer


To run external program on local computer using WSH (Window Scripting Host), you can create an object WScript.Shell and invoke its Runmethod, e.g.:

1
2
3
Dim Obj
Set Obj = CreateObject("WScript.Shell")
Obj.Run "notepad.exe"
Dim Obj
Set Obj = CreateObject("WScript.Shell")
Obj.Run "notepad.exe"

To run external program on remote computer (e.g. in local area network), you can use the following VBScript Function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Function startExe(sstrComputer,sstrEXE)
'Starts a process on a machine
'Input: sstrComputer = machine name (use "." for local)
'Input: sstrExe = Exe or command to execute (can pass full command line)
'Output: boolean
'Optional: Declare startExeStatus as a global variable to get status text
    startExe = False 
    Set sobjWMIService = GetObject("winmgmts:\\" & sstrComputer & "\root\cimv2:Win32_Process")
    sintReturn = sobjWMIService.Create(sstrEXE, null, null, sintProcessID)
    Select Case sintReturn
        Case 0 'Successful Completion
            startExe = True
            startExeStatus = "Successful Completion"
        Case 2 'Access Denied
            startExe = False
            startExeStatus = "Access Denied"
        Case 3 'Insufficient Privilege
            startExe = False
            startExeStatus = "Insufficient Privilege"
        Case 8 'Unknown Failure
            startExe = False
            startExeStatus = "Unknown Failure"
        Case 9 'Path not found
            startExe = False
            startExeStatus = "Path not found"
        Case 21 'Invalid Parameter
            startExe = False
            startExeStatus = "Invalid Parameter"
        Case Else 
            startExe = False
            startExeStatus = "Error code " & sintReturn & " not found"
    End Select 
End Function 
Function startExe(sstrComputer,sstrEXE)
'Starts a process on a machine
'Input: sstrComputer = machine name (use "." for local)
'Input: sstrExe = Exe or command to execute (can pass full command line)
'Output: boolean
'Optional: Declare startExeStatus as a global variable to get status text
    startExe = False 
    Set sobjWMIService = GetObject("winmgmts:\\" & sstrComputer & "\root\cimv2:Win32_Process")
    sintReturn = sobjWMIService.Create(sstrEXE, null, null, sintProcessID)
    Select Case sintReturn
        Case 0 'Successful Completion
            startExe = True
            startExeStatus = "Successful Completion"
        Case 2 'Access Denied
            startExe = False
            startExeStatus = "Access Denied"
        Case 3 'Insufficient Privilege
            startExe = False
            startExeStatus = "Insufficient Privilege"
        Case 8 'Unknown Failure
            startExe = False
            startExeStatus = "Unknown Failure"
        Case 9 'Path not found
            startExe = False
            startExeStatus = "Path not found"
        Case 21 'Invalid Parameter
            startExe = False
            startExeStatus = "Invalid Parameter"
        Case Else 
            startExe = False
            startExeStatus = "Error code " & sintReturn & " not found"
    End Select 
End Function 

To run a local command, use “.” for the first parameter, for example, startExe(“.”, “notepad.exe”).

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
256 words
Last Post: The Ultimate Famicom Game Cartridge - N8 Everdrive - Installed on BBG Famiclone
Next Post: C/C++ Coding Exercise - Unique Paths II - Dynamic Programming with Obstacles - Leetcode Online Judge - DP with Constraints

The Permanent URL is: VBScript Function to Run Program at Remote Computer

6 Comments

  1. sangv
  2. sangv
      • sangv
  3. Peter

Leave a Reply