How to execute program on remote computer
Several days ago I faced with a simple task - I had to execute program (automated QTP test in my case) on several remote computers.
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:

  1. Win32_Process Class

  2. 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.

  1. 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:


    1. strComputer = "."

    2. strCommand = "notepad.exe"


    3. Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2")

    4. Set objProcess = objWMIService.Get("Win32_Process")


    5. errReturn = objProcess.Create(strCommand, null, null, intProcessID)


    6. If errReturn = 0 Then

    7. Wscript.Echo "notepad.exe was started with a process ID: " & intProcessID

    8. Else

    9. Wscript.Echo "notepad.exe could not be started due to error: " & errReturn

    10. End If



    String strComputer = "." means "local computer".
    So, the result of notepad starting on my local computer was:

  2. 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:

  3. 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.

  4. 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 :


    1. strComputer = "."

    2. strCommand = "notepad.exe"


    3. Const INTERVAL = "n"

    4. Const MINUTES = 1


    5. Set objWMIService = GetObject("winmgmts:\" & strComputer & "rootcimv2")

    6. Set objScheduledJob = objWMIService.Get("Win32_ScheduledJob")

    7. Set objSWbemDateTime = CreateObject("WbemScripting.SWbemDateTime")


    8. objSWbemDateTime.SetVarDate(DateAdd(INTERVAL, MINUTES, Now()))

    9. errReturn = objScheduledJob.Create(strCommand, objSWbemDateTime.Value, False, 0, 0, True, intJobID)


    10. If errReturn = 0 Then

    11. Wscript.Echo "notepad.exe was started with a process ID: " & intJobID

    12. Else

    13. Wscript.Echo "notepad.exe could not be started due to error: " & errReturn

    14. 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

Asian Tour Trip © 2014 - Designed by Templateism, Distributed By Blogger Templates | Templatelib

Contact us

Powered by Blogger.