haripotter’s technorati

Just another WordPress.com weblog

Executing a command on a remote windows system using WMI

Usually we use psexec tool from sysinternals to execute any program on a remote system. There is another way to execute a command on a remote windows system via WMI (Windows Management Instrumentation). This function would be beneficial to QTP automators because its a vbscript function and can be used in their function libraries (after some modifications). All you have to do is call this function with the target system name, username, password and the command to execute. The output would be the process if of the process that was initated on the target system. Have fun…

 

Function RemoteExecute(strServer, strUser, strPassword, strCommand)

Dim objLocator , objWMIService

wbemImpersonationLevelImpersonate = 3
wbemAuthenticationLevelPktPrivacy = 6
RemoteExecute = -1

Set objLocator = CreateObject("WbemScripting.SWbemLocator")

On Error Resume Next
Set objWMIService = objLocator.ConnectServer(strServer,"root\cimv2", strUser,strPassword)

objWMIService.Security_.ImpersonationLevel = wbemImpersonationLevelImpersonate
objWMIService.Security_.AuthenticationLevel = wbemAuthenticationLevelPktPrivacy

If Err.Number <> 0 Then
WScript.Echo "Failed to connect to " &strServer, "Error # " & CStr(Err.Number) & " " & Err.Description & vbcrlf & _
"Please check if " & strServer & " is pingable from this client & credentials are correct"
Err.Clear
On Error GoTo 0
RemoteExecute = -1

Set objWMIService = nothing
Set objLocator = nothing
Exit function
end if

      ' Configure the process to show a window
      Set objStartup = objWMIService.Get("Win32_ProcessStartup")
      Set objConfig = objStartup.SpawnInstance_
      objConfig.ShowWindow = SW_NORMAL
       
        Set Process = objWMIService.Get("Win32_Process")
       
      'Process.Create Syntax: ' 
      '       uint32 Create(
      '[in]   string CommandLine,
      '[in]   string CurrentDirectory,
      '[in]   Win32_ProcessStartup ProcessStartupInformation,
      '[out]  uint32 ProcessId
      ');
    
     'Return code Description
      '0     Successful Completion
      '2     Access Denied
      '3     Insufficient Privilege
      '8     Unknown failure
      '9     Path Not Found
      '21    Invalid Parameter
   
        intReturn = Process.Create(strCommand,NULL, objConfig, intProcessID)

        If intReturn <> 0 Then
          Wscript.Echo "Process could not be created." & _
        vbNewLine & "Command line: " & strCommand & _
        vbNewLine & "Return value: " & intReturn
            Wscript.Quit
        Else
          Wscript.Echo "Process created." & _
        vbNewLine & "Command line: " & strCommand & _
        vbNewLine & "Process ID: " & intProcessID
           RemoteExecute = intProcessID
        End If
     

Set objWMIService = nothing
Set objLocator = nothing
 
End Function

July 25, 2008 - Posted by haripotter | HP Quality Center and QTP | , , , | 3 Comments

3 Comments »

  1. ‘Win32_Process’ class can be used for running of non-interactive application.
    If you plan to run an interactive application on remote computer, you can use ‘Win32_ScheduledJob’ Class.

    I have described it here:
    How to execute program on remote computer?

    I hope, this article will be helpful for you.

    Comment by QTP (QuickTest Professional) | July 26, 2008 | Reply

  2. Thank you Dmitry. Appreciate it.

    Comment by haripotter | July 27, 2008 | Reply

  3. Hi,

    I have a VB script(say forceupdates.vbs.) which is used to install approved windows updates forcefully on servers.

    When I run the script locally it works fine.I want to run this script on couple of remote servers but I don’t know.

    Could you please help me?

    PS:Using PSEXEC doesn’t help.

    Regards,
    Umesh

    Comment by Umesh | December 16, 2008 | Reply


Leave a comment