Script to Open a batch file on a remote computer

别等时光非礼了梦想. 提交于 2019-12-11 21:01:18

问题


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"

来源:https://stackoverflow.com/questions/26020004/script-to-open-a-batch-file-on-a-remote-computer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!