0

I currently manually open a Remote Desktop and run a .bat file by double clicking it. I have found the following code on this site or maybe elsewhere that should allow me to automate this. This code works when I try to open Notepad or calc on the remote computer, but it wont run my .bat file. I get "process could not be started due to error 8". I checked and error 8 means "unknown failure". PSEXEC is not an option for me. Any help would be appreciated. Thanks.

Const ForWriting = 2
Const ForAppending = 8
Const NormalWindow = 5
Set fs = CreateObject("Scripting.FileSystemObject")
Dim CurrentDate
CurrentDate = Date
DateTime = CurrentDate & "," & CurrentTime

On Error Resume Next

'-----------------------------------------------------------

 remote_machine_name = "RemoteComputerName"

Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objService = objLocator.ConnectServer(remote_machine_name, "Root\DEFAULT")

If Err.Number <> 0 Then
WScript.Echo "Error " & Err.Number & ": " & Err.Description
Err.Clear
Else

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & remote_machine_name & "\root\cimv2:Win32_Process")

Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = NormalWindow

Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process")

'-------This line works fine
errReturn = objProcess.Create("notepad.exe", NULL, objConfig, intProcessID)

'-------This line doesn't work
'errReturn = objProcess.Create("XX.bat", "c:\users\Name\desktop\", objConfig, intProcessID)

'-------Neither does this line
'errReturn = objProcess.Create("XX.bat", "c:\users\Name\desktop\", NULL, intProcessID)

If errReturn = 0 Then
    Wscript.Echo "Process started with a process ID of " & intProcessID & "."
Else
    Wscript.Echo "Process could not be started due to error " & errReturn & "."
End If
End If

WScript.Echo "Completed"
user2358848
  • 31
  • 1
  • 5
  • I'm not where I can verify, but maybe it wants to create a process directly, rather than recognizing what process to launch based on the .bat extension. Have you tried something like `objProcess.Create("cmd.exe /C XX.bat", null, null, pid)`? – lordjeb Sep 24 '14 at 15:24
  • Thanks. The error is gone. But now I see the green batch winmdow flash for 1/2 second, but the batch file is not run. I don't see the file it normally produces. – user2358848 Sep 24 '14 at 16:13
  • I would run something like procmon to see what it's actually doing. Perhaps you need to specify a full path to the batch file? You can also run cmd.exe with /K instead of /C and the process should stick around after running the batch file, perhaps displaying an error you can use. – lordjeb Sep 24 '14 at 17:23
  • tried the K and it hepled. These are he errors I am still getting. – user2358848 Sep 25 '14 at 01:59
  • 'Not recognized...as command, program or batch file errReturn = objProcess.Create("cmd.exe /K c:\users\name\desktop\XX.bat", null, NULL, intProcessID) and this one: 'Subdirectory or file .bat already exists errReturn = objProcess.Create("cmd.exe /K XX.bat", NULL, Null, intProcessid) – user2358848 Sep 25 '14 at 02:02

0 Answers0