Script to Open a batch file on a remote computer

2019-08-31 23:37发布

问题:

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"