Monday, July 4, 2011

JAVA WMI ExecNotificationQuery Example Using InstanceDeletionEvent

You can download the scripting program for generating these scripts at:

http://www.planetmps.com/wmiscriptsENQ_D.html

/*
 * Main.java
 *
 * Created on October 22, 2007, 7:52 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package javaapplication1;
import java.io.Console;
import org.jawin.DispatchPtr;
import org.jawin.UnknownPtr;
import org.jawin.IEnumVariant;
import org.jawin.win32.*;
import org.jawin.COMPtr;
/**
 *
 * @author Richard
 */
public class Main {
    /** Creates a new instance of Main */
    public Main() {
    }
    /**
     * @param args the command line arguments
     */
     public static String GetValue(DispatchPtr prop, DispatchPtr obj)
     {
        try
        {
           Ole32.CoInitialize();
            String Name = (String)prop.get("Name");
            String tempstr = (String)obj.invoke("GetObjectText_", 0);
            int pos = tempstr.indexOf(Name + " = ");
            if (pos > 0)
            {
                pos = pos + Name.length();
                pos = pos + 2;
                tempstr = tempstr.substring(pos, tempstr.length());
                pos = tempstr.indexOf(";");
                tempstr = tempstr.substring(0, pos);
                tempstr = tempstr.replace("{", "");
                tempstr = tempstr.replace("}", "");
                tempstr = tempstr.replace("\", "");
                tempstr = tempstr.trim();
                String cimtype = prop.get("CIMType").toString();
                if (cimtype.equals("101")
                {
                    if(tempstr.length() > 13)
                    {
                        tempstr = tempstr.substring(4,6) + "/" + tempstr.substring(6,8) + "/" + tempstr.substring(0,4) + " " + tempstr.substring(8,10) + ":" + tempstr.substring(10,12) + ":" + tempstr.substring(12,14);
                    }
                }
                Ole32.CoUninitialize();
                return tempstr;
            }
            else
            {
                Ole32.CoUninitialize();
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
            return "";
     }
    public static void main(String[] args) {
       Try
       {
            Ole32.CoInitialize();
            DispatchPtr l = new DispatchPtr("WbemScripting.SWbemLocator");
            DispatchPtr services = (DispatchPtr)l.invoke("ConnectServer");
            DispatchPtr objs = (DispatchPtr)services.invoke("ExecNotificationQuery", "Select * From __InstanceOperationEvent WITHIN 1 where targetInstance ISA 'Win32_Process'");
            UnknownPtr oEnum = (UnknownPtr)objs.get("_NewEnum" & vbcrlf;
            IEnumVariant objEnum = (IEnumVariant)oEnum.queryInterface(IEnumVariant.class);
            int x=1;
            String mystr ="";
            Object[] tempobj = new Object[1];
            while(objEnum.Next(1, tempobj) == 1)
            {
                DispatchPtr obj = (DispatchPtr)tempobj[0];
                DispatchPtr propset = (DispatchPtr)obj.invoke("Properties_");
                UnknownPtr pEnum = (UnknownPtr)objs.get("_NewEnum" & vbcrlf;
                IEnumVariant propEnum = (IEnumVariant)pEnum.queryInterface(IEnumVariant.class);
                int y=1;
                Object[] tempProp = new Object[1];
                while(propEnum.Next(1, tempProp) == 1)
                {
                     DispatchPtr prop = (DispatchPtr)tempProp[0];
                }
                break;
            }
            Ole32.CoUninitialize();
       }
       catch(Exception e)
       {
        e.printStackTrace();
       }
       }
}

JAVA WMI ExecNotificationQuery Example Using InstanceCreationEvent

You can download the scripting program for generating these scripts at:

http://www.planetmps.com/wmiscriptsENQ_C.html

/*
 * Main.java
 *
 * Created on October 22, 2007, 7:52 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package javaapplication1;
import java.io.Console;
import org.jawin.DispatchPtr;
import org.jawin.UnknownPtr;
import org.jawin.IEnumVariant;
import org.jawin.win32.*;
import org.jawin.COMPtr;
/**
 *
 * @author Richard
 */
public class Main {
    /** Creates a new instance of Main */
    public Main() {
    }
    /**
     * @param args the command line arguments
     */
     public static String GetValue(DispatchPtr prop, DispatchPtr obj)
     {
        try
        {
           Ole32.CoInitialize();
            String Name = (String)prop.get("Name");
            String tempstr = (String)obj.invoke("GetObjectText_", 0);
            int pos = tempstr.indexOf(Name + " = ");
            if (pos > 0)
            {
                pos = pos + Name.length();
                pos = pos + 2;
                tempstr = tempstr.substring(pos, tempstr.length());
                pos = tempstr.indexOf(";");
                tempstr = tempstr.substring(0, pos);
                tempstr = tempstr.replace("{", "");
                tempstr = tempstr.replace("}", "");
                tempstr = tempstr.replace("\", "");
                tempstr = tempstr.trim();
                String cimtype = prop.get("CIMType").toString();
                if (cimtype.equals("101")
                {
                    if(tempstr.length() > 13)
                    {
                        tempstr = tempstr.substring(4,6) + "/" + tempstr.substring(6,8) + "/" + tempstr.substring(0,4) + " " + tempstr.substring(8,10) + ":" + tempstr.substring(10,12) + ":" + tempstr.substring(12,14);
                    }
                }
                Ole32.CoUninitialize();
                return tempstr;
            }
            else
            {
                Ole32.CoUninitialize();
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
            return "";
     }
    public static void main(String[] args) {
       Try
       {
            Ole32.CoInitialize();
            DispatchPtr l = new DispatchPtr("WbemScripting.SWbemLocator");
            DispatchPtr services = (DispatchPtr)l.invoke("ConnectServer");
            DispatchPtr objs = (DispatchPtr)services.invoke("ExecNotificationQuery", "Select * From __InstanceCreationEvent WITHIN 1 where targetInstance ISA 'Win32_Process'");
            UnknownPtr oEnum = (UnknownPtr)objs.get("_NewEnum" & vbcrlf;
            IEnumVariant objEnum = (IEnumVariant)oEnum.queryInterface(IEnumVariant.class);
            int x=1;
            String mystr ="";
            Object[] tempobj = new Object[1];
            while(objEnum.Next(1, tempobj) == 1)
            {
                DispatchPtr obj = (DispatchPtr)tempobj[0];
                DispatchPtr propset = (DispatchPtr)obj.invoke("Properties_");
                UnknownPtr pEnum = (UnknownPtr)objs.get("_NewEnum" & vbcrlf;
                IEnumVariant propEnum = (IEnumVariant)pEnum.queryInterface(IEnumVariant.class);
                int y=1;
                Object[] tempProp = new Object[1];
                while(propEnum.Next(1, tempProp) == 1)
                {
                     DispatchPtr prop = (DispatchPtr)tempProp[0];
                }
                break;
            }
            Ole32.CoUninitialize();
       }
       catch(Exception e)
       {
        e.printStackTrace();
       }
       }
}

JAVA WMI ExecNotificationQuery Example Using InstanceModificationEvent

You can download the scripting program for generating these scripts at:

http://www.planetmps.com/wmiscriptsENQ_M.html

/*
 * Main.java
 *
 * Created on October 22, 2007, 7:52 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package javaapplication1;
import java.io.Console;
import org.jawin.DispatchPtr;
import org.jawin.UnknownPtr;
import org.jawin.IEnumVariant;
import org.jawin.win32.*;
import org.jawin.COMPtr;
/**
 *
 * @author Richard
 */
public class Main {
    /** Creates a new instance of Main */
    public Main() {
    }
    /**
     * @param args the command line arguments
     */
     public static String GetValue(DispatchPtr prop, DispatchPtr obj)
     {
        try
        {
           Ole32.CoInitialize();
            String Name = (String)prop.get("Name");
            String tempstr = (String)obj.invoke("GetObjectText_", 0);
            int pos = tempstr.indexOf(Name + " = ");
            if (pos > 0)
            {
                pos = pos + Name.length();
                pos = pos + 2;
                tempstr = tempstr.substring(pos, tempstr.length());
                pos = tempstr.indexOf(";");
                tempstr = tempstr.substring(0, pos);
                tempstr = tempstr.replace("{", "");
                tempstr = tempstr.replace("}", "");
                tempstr = tempstr.replace("\", "");
                tempstr = tempstr.trim();
                String cimtype = prop.get("CIMType").toString();
                if (cimtype.equals("101")
                {
                    if(tempstr.length() > 13)
                    {
                        tempstr = tempstr.substring(4,6) + "/" + tempstr.substring(6,8) + "/" + tempstr.substring(0,4) + " " + tempstr.substring(8,10) + ":" + tempstr.substring(10,12) + ":" + tempstr.substring(12,14);
                    }
                }
                Ole32.CoUninitialize();
                return tempstr;
            }
            else
            {
                Ole32.CoUninitialize();
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
            return "";
     }
    public static void main(String[] args) {
       Try
       {
            Ole32.CoInitialize();
            DispatchPtr l = new DispatchPtr("WbemScripting.SWbemLocator");
            DispatchPtr services = (DispatchPtr)l.invoke("ConnectServer");
            DispatchPtr objs = (DispatchPtr)services.invoke("ExecNotificationQuery", "Select * From __InstanceModificationEvent WITHIN 1 where targetInstance ISA 'Win32_Process'");
            UnknownPtr oEnum = (UnknownPtr)objs.get("_NewEnum" & vbcrlf;
            IEnumVariant objEnum = (IEnumVariant)oEnum.queryInterface(IEnumVariant.class);
            int x=1;
            String mystr ="";
            Object[] tempobj = new Object[1];
            while(objEnum.Next(1, tempobj) == 1)
            {
                DispatchPtr obj = (DispatchPtr)tempobj[0];
                DispatchPtr propset = (DispatchPtr)obj.invoke("Properties_");
                UnknownPtr pEnum = (UnknownPtr)objs.get("_NewEnum" & vbcrlf;
                IEnumVariant propEnum = (IEnumVariant)pEnum.queryInterface(IEnumVariant.class);
                int y=1;
                Object[] tempProp = new Object[1];
                while(propEnum.Next(1, tempProp) == 1)
                {
                     DispatchPtr prop = (DispatchPtr)tempProp[0];
                }
                break;
            }
            Ole32.CoUninitialize();
       }
       catch(Exception e)
       {
        e.printStackTrace();
       }
       }
}

JAVA WMI ExecNotificationQuery Example Using InstanceOperationEvent

You can download the scripting program for generating these scripts at:

http://www.planetmps.com/wmiscriptsENQ_O.html

/*
 * Main.java
 *
 * Created on October 22, 2007, 7:52 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package javaapplication1;
import java.io.Console;
import org.jawin.DispatchPtr;
import org.jawin.UnknownPtr;
import org.jawin.IEnumVariant;
import org.jawin.win32.*;
import org.jawin.COMPtr;
/**
 *
 * @author Richard
 */
public class Main {
    /** Creates a new instance of Main */
    public Main() {
    }
    /**
     * @param args the command line arguments
     */
     public static String GetValue(DispatchPtr prop, DispatchPtr obj)
     {
        try
        {
           Ole32.CoInitialize();
            String Name = (String)prop.get("Name");
            String tempstr = (String)obj.invoke("GetObjectText_", 0);
            int pos = tempstr.indexOf(Name + " = ");
            if (pos > 0)
            {
                pos = pos + Name.length();
                pos = pos + 2;
                tempstr = tempstr.substring(pos, tempstr.length());
                pos = tempstr.indexOf(";");
                tempstr = tempstr.substring(0, pos);
                tempstr = tempstr.replace("{", "");
                tempstr = tempstr.replace("}", "");
                tempstr = tempstr.replace("\", "");
                tempstr = tempstr.trim();
                String cimtype = prop.get("CIMType").toString();
                if (cimtype.equals("101")
                {
                    if(tempstr.length() > 13)
                    {
                        tempstr = tempstr.substring(4,6) + "/" + tempstr.substring(6,8) + "/" + tempstr.substring(0,4) + " " + tempstr.substring(8,10) + ":" + tempstr.substring(10,12) + ":" + tempstr.substring(12,14);
                    }
                }
                Ole32.CoUninitialize();
                return tempstr;
            }
            else
            {
                Ole32.CoUninitialize();
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
            return "";
     }
    public static void main(String[] args) {
       Try
       {
            Ole32.CoInitialize();
            DispatchPtr l = new DispatchPtr("WbemScripting.SWbemLocator");
            DispatchPtr services = (DispatchPtr)l.invoke("ConnectServer");
            DispatchPtr objs = (DispatchPtr)services.invoke("ExecNotificationQuery", "Select * From __InstanceOperationEvent WITHIN 1 where targetInstance ISA 'Win32_Process'");
            UnknownPtr oEnum = (UnknownPtr)objs.get("_NewEnum" & vbcrlf;
            IEnumVariant objEnum = (IEnumVariant)oEnum.queryInterface(IEnumVariant.class);
            int x=1;
            String mystr ="";
            Object[] tempobj = new Object[1];
            while(objEnum.Next(1, tempobj) == 1)
            {
                DispatchPtr obj = (DispatchPtr)tempobj[0];
                DispatchPtr propset = (DispatchPtr)obj.invoke("Properties_");
                UnknownPtr pEnum = (UnknownPtr)objs.get("_NewEnum" & vbcrlf;
                IEnumVariant propEnum = (IEnumVariant)pEnum.queryInterface(IEnumVariant.class);
                int y=1;
                Object[] tempProp = new Object[1];
                while(propEnum.Next(1, tempProp) == 1)
                {
                     DispatchPtr prop = (DispatchPtr)tempProp[0];
                }
                break;
            }
            Ole32.CoUninitialize();
       }
       catch(Exception e)
       {
        e.printStackTrace();
       }
       }
}

JAVA WMI ExecQuery Example

You can download the scripting program for generating these scripts at:

http://www.planetmps.com/wmiscriptsexecquery.html

/*
* Main.java
*
* Created on October 22, 2007, 7:52 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package javaapplication1;
import java.io.Console;
import org.jawin.DispatchPtr;
import org.jawin.UnknownPtr;
import org.jawin.IEnumVariant;
import org.jawin.win32.*;
import org.jawin.COMPtr;
/**
*
* @author Richard
*/
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static String GetValue(DispatchPtr prop, DispatchPtr obj)
{
try
{
Ole32.CoInitialize();
String Name = (String)prop.get("Name");
String tempstr = (String)obj.invoke("GetObjectText_", 0);
int pos = tempstr.indexOf(Name + " = ");
if (pos > 0)
{
pos = pos + Name.length();
pos = pos + 2;
tempstr = tempstr.substring(pos, tempstr.length());
pos = tempstr.indexOf(";");
tempstr = tempstr.substring(0, pos);
tempstr = tempstr.replace("{", "");
tempstr = tempstr.replace("}", "");
tempstr = tempstr.replace("\", "");
tempstr = tempstr.trim();
String cimtype = prop.get("CIMType").toString();
if (cimtype.equals("101")
{
if(tempstr.length() > 13)
{
tempstr = tempstr.substring(4,6) + "/" + tempstr.substring(6,8) + "/" + tempstr.substring(0,4) + " " + tempstr.substring(8,10) + ":" + tempstr.substring(10,12) + ":" + tempstr.substring(12,14);
}
}
Ole32.CoUninitialize();
return tempstr;
}
else
{
Ole32.CoUninitialize();
}

} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public static void main(String[] args) {
Try
{
Ole32.CoInitialize();
DispatchPtr l = new DispatchPtr("WbemScripting.SWbemLocator");
DispatchPtr services = (DispatchPtr)l.invoke("ConnectServer");
DispatchPtr objs = (DispatchPtr)services.invoke("ExecQuery", "Select * From Win32_Service");
UnknownPtr oEnum = (UnknownPtr)objs.get("_NewEnum" & vbcrlf;
IEnumVariant objEnum = (IEnumVariant)oEnum.queryInterface(IEnumVariant.class);
int x=1;
String mystr ="";
Object[] tempobj = new Object[1];
while(objEnum.Next(1, tempobj) == 1)
{
DispatchPtr obj = (DispatchPtr)tempobj[0];
DispatchPtr propset = (DispatchPtr)obj.invoke("Properties_");
UnknownPtr pEnum = (UnknownPtr)objs.get("_NewEnum" & vbcrlf;
IEnumVariant propEnum = (IEnumVariant)pEnum.queryInterface(IEnumVariant.class);
int y=1;
Object[] tempProp = new Object[1];
while(propEnum.Next(1, tempProp) == 1)
{
DispatchPtr prop = (DispatchPtr)tempProp[0];
}
break;
}
Ole32.CoUninitialize();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

JAVA WMI InstancesOf Example

You can download the scripting program for generating these scripts at:

http://www.planetmps.com/wmiscriptsinstancesof.html

/*
* Main.java
*
* Created on October 22, 2007, 7:52 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package javaapplication1;
import java.io.Console;
import org.jawin.DispatchPtr;
import org.jawin.UnknownPtr;
import org.jawin.IEnumVariant;
import org.jawin.win32.*;
import org.jawin.COMPtr;
/**
*
* @author Richard
*/
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static String GetValue(DispatchPtr prop, DispatchPtr obj)
{
try
{
Ole32.CoInitialize();
String Name = (String)prop.get("Name");
String tempstr = (String)obj.invoke("GetObjectText_", 0);
int pos = tempstr.indexOf(Name + " = ");
if (pos > 0)
{
pos = pos + Name.length();
pos = pos + 2;
tempstr = tempstr.substring(pos, tempstr.length());
pos = tempstr.indexOf(";");
tempstr = tempstr.substring(0, pos);
tempstr = tempstr.replace("{", "");
tempstr = tempstr.replace("}", "");
tempstr = tempstr.replace("\", "");
tempstr = tempstr.trim();
String cimtype = prop.get("CIMType").toString();
if (cimtype.equals("101")
{
if(tempstr.length() > 13)
{
tempstr = tempstr.substring(4,6) + "/" + tempstr.substring(6,8) + "/" + tempstr.substring(0,4) + " " + tempstr.substring(8,10) + ":" + tempstr.substring(10,12) + ":" + tempstr.substring(12,14);
}
}
Ole32.CoUninitialize();
return tempstr;
}
else
{
Ole32.CoUninitialize();
}

} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public static void main(String[] args) {
Try
{
Ole32.CoInitialize();
DispatchPtr l = new DispatchPtr("WbemScripting.SWbemLocator");
DispatchPtr services = (DispatchPtr)l.invoke("ConnectServer");
DispatchPtr objs = (DispatchPtr)services.invoke("InstancesOf", "Win32_Service");
UnknownPtr oEnum = (UnknownPtr)objs.get("_NewEnum");
IEnumVariant objEnum = (IEnumVariant)oEnum.queryInterface(IEnumVariant.class);
int x=1;
String mystr ="";
Object[] tempobj = new Object[1];
while(objEnum.Next(1, tempobj) == 1)
{
DispatchPtr obj = (DispatchPtr)tempobj[0];
DispatchPtr propset = (DispatchPtr)obj.invoke("Properties_");
UnknownPtr pEnum = (UnknownPtr)objs.get("_NewEnum" & vbcrlf;
IEnumVariant propEnum = (IEnumVariant)pEnum.queryInterface(IEnumVariant.class);
int y=1;
Object[] tempProp = new Object[1];
while(propEnum.Next(1, tempProp) == 1)
{
DispatchPtr prop = (DispatchPtr)tempProp[0];
}
break;
}
Ole32.CoUninitialize();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

JAVA WMI Instances Example

You can download the scripting program for generating these scripts at:

http://www.planetmps.com/wmiscriptsinstances.html

/*
* Main.java
*
* Created on October 22, 2007, 7:52 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package javaapplication1;
import java.io.Console;
import org.jawin.DispatchPtr;
import org.jawin.UnknownPtr;
import org.jawin.IEnumVariant;
import org.jawin.win32.*;
import org.jawin.COMPtr;
/**
*
* @author Richard
*/
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static String GetValue(DispatchPtr prop, DispatchPtr obj)
{
try
{
Ole32.CoInitialize();
String Name = (String)prop.get("Name");
String tempstr = (String)obj.invoke("GetObjectText_", 0);
int pos = tempstr.indexOf(Name + " = ");
if (pos > 0)
{
pos = pos + Name.length();
pos = pos + 2;
tempstr = tempstr.substring(pos, tempstr.length());
pos = tempstr.indexOf(";");
tempstr = tempstr.substring(0, pos);
tempstr = tempstr.replace("{", "");
tempstr = tempstr.replace("}", "");
tempstr = tempstr.replace("\", "");
tempstr = tempstr.trim();
String cimtype = prop.get("CIMType").toString();
if (cimtype.equals("101")
{
if(tempstr.length() > 13)
{
tempstr = tempstr.substring(4,6) + "/" + tempstr.substring(6,8) + "/" + tempstr.substring(0,4) + " " + tempstr.substring(8,10) + ":" + tempstr.substring(10,12) + ":" + tempstr.substring(12,14);
}
}
Ole32.CoUninitialize();
return tempstr;
}
else
{
Ole32.CoUninitialize();
}

} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public static void main(String[] args) {
Try
{
Ole32.CoInitialize();
DispatchPtr l = new DispatchPtr("WbemScripting.SWbemLocator");
DispatchPtr services = (DispatchPtr)l.invoke("ConnectServer");
DispatchPtr ob = (DispatchPtr)services.invoke("Get", "Win32_BIOS");
DispatchPtr objs = (DispatchPtr)ob.get("Instances_");
UnknownPtr oEnum = (UnknownPtr)objs.get("_NewEnum" & vbcrlf;
IEnumVariant objEnum = (IEnumVariant)oEnum.queryInterface(IEnumVariant.class);
int x=1;
String mystr ="";
Object[] tempobj = new Object[1];
while(objEnum.Next(1, tempobj) == 1)
{
DispatchPtr obj = (DispatchPtr)tempobj[0];
DispatchPtr propset = (DispatchPtr)obj.invoke("Properties_");
UnknownPtr pEnum = (UnknownPtr)objs.get("_NewEnum" & vbcrlf;
IEnumVariant propEnum = (IEnumVariant)pEnum.queryInterface(IEnumVariant.class);
int y=1;
Object[] tempProp = new Object[1];
while(propEnum.Next(1, tempProp) == 1)
{
DispatchPtr prop = (DispatchPtr)tempProp[0];
}
break;
}
Ole32.CoUninitialize();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}