Forum Discussion

densaface's avatar
densaface
New Contributor
13 years ago

How restart remote computer by WMI

I'm using a license TestComplete 7.52... Script on Delphi:




  Locator := Sys.OleObject ('WbemScripting.SWbemLocator');

  Locator.Security_.ImpersonationLevel := 3;

  WmiService := Locator.ConnectServer (Server, 'root\cimv2', 'Administrator', '1q2w3eQAZ');

  NewJob := WmiService.Get('Win32_OperatingSystem');

  NewJob.Win32Shutdown(2); // this is error "SWbemObjectEx Invalid method Parameter(s)"



Why is invalid? How to write correctly


  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Denis,



    Two things:



    1. The Win32_OperatingSystem method description says:
    The calling process must have the SE_SHUTDOWN_NAME privilege.
    So, you need to add the following line to your script before calling Locator.ConnectServer:





    2. If the remote computer uses Windows Vista or 7, you need to obtain the Win32_OperatingSystem object in the following way:



    If it uses Windows XP, you need to specify its Win32_OperatingSystem.Name property value in the Get method call, like this:



    To learn this value, you can execute the following command line on the remote computer:

    wmic os get Name
    Note that you need to double the backslashes in this value when using it in the Get method call.





    By the way, in TestComplete 8, it's just two lines of code using the built-in WMI.RestartComputer method:

    WMI.ConnectToComputer(Server, 'Administrator', '1q2w3eQAZ');

    WMI.RestartComputer;
  • densaface's avatar
    densaface
    New Contributor
    Thanks a lot. Your first method is success. 




      Locator := Sys.OleObject ('WbemScripting.SWbemLocator');

      Locator.Security_.Privileges.AddAsString('SeShutdownPrivilege');

      WmiService := Locator.ConnectServer (Server, 'root\cimv2', 'Administrator', '1q2w3eQAZ');

      NewJob := WmiService.Get('Win32_OperatingSystem.Name="Microsoft Windows Server 2003 Datacenter x64 Edition|C:\\WINDOWS|\\Device\\Harddisk0\\Partition1"');

      NewJob.Win32Shutdown('2');