My goal was simple - if I can execute scripts on my computer, then why don't we execute them on remote computers?
I think, this task is very interesting and practical, isn't it? :)
There was one condition. I tried to use "native" Windows solutions, which do not require additional installed software or libraries. I didn't want to use external soft, files, etc.
As a result, I found two solutions. Both of them use WMI:
- Win32_Process Class
- Win32_ScheduledJob Class
I will describe both of them.
Running ahead, I will say that I decided in favour of second solution (Win32_ScheduledJob class).
Let's explore these solutions, and compare them.
For clarity, I will show how to execute simple application (notepad.exe) on remote computer.
- Execute program on remote computer with Win32_Process сlass
To create and start new process, I used Create method of the Win32_Process class.
VBScript code is simple enough:- strComputer = "."
- strCommand = "notepad.exe"
- Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2")
- Set objProcess = objWMIService.Get("Win32_Process")
- errReturn = objProcess.Create(strCommand, null, null, intProcessID)
- If errReturn = 0 Then
- Wscript.Echo "notepad.exe was started with a process ID: " & intProcessID
- Else
- Wscript.Echo "notepad.exe could not be started due to error: " & errReturn
- End If
String strComputer = "." means "local computer".
So, the result of notepad starting on my local computer was: - And notepad started on my computer! Great!Then I tried execute my script on remote computer (I set strComputer = "servername"). The result was:
Script said, that the process was created.
Indeed, process was created. It was shown in Task Manager on remote computer: - But I didn't see it on a desktop of remote computer!
Further investigations shown, that:
For security reasons the Win32_Process.Create method cannot be used to start an interactive process remotely.Win32_Process class is very useful to create and execute batch tasks on remote computer. But it can be applicable for interactive processes.
That's why I preferred the second way. I mean Win32_ScheduledJob сlass. - Execute program on remote computer with Win32_ScheduledJob сlass
By analogy, to create and start new process, I used Create method of the Win32_ScheduledJob class.
VBScript code is :- strComputer = "."
- strCommand = "notepad.exe"
- Const INTERVAL = "n"
- Const MINUTES = 1
- Set objWMIService = GetObject("winmgmts:\" & strComputer & "rootcimv2")
- Set objScheduledJob = objWMIService.Get("Win32_ScheduledJob")
- Set objSWbemDateTime = CreateObject("WbemScripting.SWbemDateTime")
- objSWbemDateTime.SetVarDate(DateAdd(INTERVAL, MINUTES, Now()))
- errReturn = objScheduledJob.Create(strCommand, objSWbemDateTime.Value, False, 0, 0, True, intJobID)
- If errReturn = 0 Then
- Wscript.Echo "notepad.exe was started with a process ID: " & intJobID
- Else
- Wscript.Echo "notepad.exe could not be started due to error: " & errReturn
- End If
Create method of Win32_ScheduledJob сlass creates new scheduled job on computer. (Hm... Have I said a tautology? :) ) In script I specified the time when scheduled job should be executed - in one minute from now. For that i used standard VBScript function - DateAdd.
Hint: Some time ago I wrote an article abount time/date functions in VBScript. Read it.
The result of execution was excellent! It created scheduled jobs both on local and remote computer.
This is result for remote computer is:
In one minute, the notepad was started and shown on desktop of remote computer!
I was happy :))
I hope, these two methods of remote application execution will be very helpful for you. Computer should calculate, human should think :)
So, I shown how to force your computers to work.
Do you have any ideas on how to force a human to think? :)
Source http://motevich.blogspot.com
Reviews:
Post a Comment