Monday, July 4, 2011

Groovy 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_Thread'");
            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();
       }
       }
}

No comments:

Post a Comment