Home : Shutting Down a Managed Windows Machine Using WMI
Q56925 - HOWTO: Shutting Down a Managed Windows Machine Using WMI

Shutting Down a Managed Windows Machine Using WMI

 

Please read through the instructions in the Windows Management Instrumentation example before attempting this example.

import com.linar.jintegra.*;
import wbemdisp.*;
import java.util.*;
import java.lang.reflect.*;

public class ShutdownWindows {

   public static void main(String[] args) {

      // String managedMachine = "123.456.78.9";
      String managedMachine = "localhost";
      // String managingMachine = "123.456.78.1";
      String managingMachine = "localhost";

      try{
         com.linar.jintegra.Log.logImmediately(3, "jintegra.log");
         // com.linar.jintegra.AuthInfo.setDefault("WORKGROUP", "Administrator", "Password");
         wbemdisp.SWbemLocator wLoc = new wbemdisp.SWbemLocator(managingMachine); 

         ISWbemServices wbemServices = null;
         wbemServices = wLoc.connectServer(
            managedMachine,
            null,
            null,
            null,
            null,
            null,
            0,
            null
         );
         /* wbemServices = wLoc.connectServer(
            managedMachine,
            "root\\CIMV2",
            "Administrator",
            "Password",
            null,
            null,
            0,
            null
         );
         */ 
         try{
            /* Win32_OperatingSystem Reference:
             * http://msdn.microsoft.com/library/en-us/wmisdk/wmi/win32_operatingsystem.asp
             */
            ISWbemObject oProcess= new SWbemObject(wbemServices.get("Win32_OperatingSystem",0,null));
            String query = "Select * from Win32_OperatingSystem where Primary = true";
            ISWbemObjectSet object = wbemServices.execQuery(query,"WQL",0,null);

            ISWbemObject obj = new SWbemObject(object.get_NewEnum().nextElement());
            System.out.println(obj.getObjectText_(0));

            /* Win32_OperatingSystem.Win32Shutdown
             * Function Win32Shutdown(ByVal Flags As Integer, ByVal Reserved As Integer) As Integer
             */
            ISWbemMethod method = oProcess.getMethods_().item("Win32Shutdown", 0);

            /* Constructing InParameters Objects Reference:
             * http://msdn.microsoft.com/library/en-us/wmisdk/wmi/constructing_inparameters_objects.asp
             * VB: Set oInParams = oProcess.Methods_("Win32Shutdown").InParameters.SpawnInstance_
             */
            ISWbemObject oIPs = method.getInParameters();
            ISWbemObject oInParams = oIPs.spawnInstance_(0);
            // VB: oInParam.Properties_.item("Flags") = 1
            oInParams.getProperties_().item("Flags", 0).setValue(new Integer(1));
            // VB: oInParam.Properties_.item("Reserved") = 0
            oInParams.getProperties_().item("Reserved", 0).setValue(new Integer(0));

            /*****************************************
             * For Windows NT/2000 you need to enable SeShutdownPrivilege.
             * For Windows XP you don't need to do the following.
             *
             * Set Shutdown Privilege Reference:
             * http://msdn.microsoft.com/library/en-us/wmisdk/wmi/executing_privileged_operations_using_vbscript.asp
             * Windows NT/2000 Privilege Strings Reference
             * http://msdn.microsoft.com/library/en-us/wmisdk/wmi/windows_nt_windows_2000_privilege_strings.asp
             */
            ISWbemSecurity security = obj.getSecurity_();
            SWbemPrivilegeSet privilegeSet = new SWbemPrivilegeSet(security.getPrivileges());
            privilegeSet.add(WbemPrivilegeEnum.wbemPrivilegeShutdown, true);
            /*****************************************/

            // VB: Set objOutParams = wbemServices.ExecMethod_("Win32Shutdown", objInParam)
            ISWbemObject oOutParams = obj.execMethod_("Win32Shutdown", oInParams, 0, null);

         } catch(java.util.NoSuchElementException nselex){
            System.out.println("No more processes found");
            nselex.printStackTrace();
         }
      } catch(AutomationException aex){
         System.out.println("WMI has raised an exception: " + aex.toString());
         aex.printStackTrace();
      } catch(Exception ex){
         ex.printStackTrace();
      } finally{
         // Release all the COM objects:
         com.linar.jintegra.Cleaner.releaseAll();
      } 
   }
}
Related Articles
No Related Articles Available.

Article Attachments
No Attachments Available.

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
Tell us why you rated the content this way. (optional)
 
Approved Comments...
No user comments available for this article.
Created on 6/23/2006.
Last Modified on 7/11/2006.
Last Modified by No Author Name Available!.
Article has been viewed 9024 times.
Rated 5 out of 10 based on 34 votes.
Print Article
Email Article